码迷,mamicode.com
首页 > 其他好文 > 详细

UI 03 UIViewController (视图控制器)的7个方法与视图的跳转

时间:2015-08-04 23:06:48      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

新建一个继承于UIViewController 的类
在AppDelegate.m 中写如下代码.

 //1.创建一个rootViewController对象
    RootViewController *rootVC = [[RootViewController alloc]init];
    //2.给window设置根视图控制器
    self.window.rootViewController = rootVC;
    [rootVC release];

在RootViewController.m文件中的代码如下

#import "RootViewController.h"
#define HEIGHT self.view.frame.size.height
#import "SecondViewController.h"
@interface RootViewController ()
@property(nonatomic,retain)NSMutableArray *arr;
@end

@implementation RootViewController
// UIViewController的方法完整有7个,如下:

//1.初始化方法,这个方法一般自己就调用了,不需要我们再额外调用.会初始化一些容器,比如数组,字典等.
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arr = [NSMutableArray array];
    }
    //打印出当前方法名
    NSLog(@"%s",__FUNCTION__);
    return self;
}

//2.self.view 的加载,什么时候用到什么时候触发方法.
- (void)loadView
{
    [super loadView];
    NSLog(@"%s",__FUNCTION__);
}

#pragma mark 如果想重写父类的方法,首先先用super去调用父类的方法,这样可以保证原功能不变,然后在方法里再写新添加的功能;

//3.前三个方法是同时触发的,一般代码写在这个方法中.
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor yellowColor];
//      NSLog(@"%s",__FUNCTION__);

   // 视图的跳转:
   **UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 500, 50, 40);
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10 ;
    button.backgroundColor = [UIColor cyanColor];**

}

- (void)click:(UIButton *)button
{
    //随机颜色
//    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0  blue:arc4random() % 256 / 255.0  alpha:1];  
    //创建一个secondVC的对象
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    //设置一下跳转的时候动画效果
    [secondVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    //进行跳转 
    [self presentViewController:secondVC animated:YES completion:^{

    }];
    //内存管理
    [secondVC release];  
}

//4.视图将要出现
#pragma mark 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
      NSLog(@"%s",__FUNCTION__);
}
//5.视图已经出现
#warning 这个方法是视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
      NSLog(@"%s",__FUNCTION__);
}
//6.视图将要消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
      NSLog(@"%s",__FUNCTION__);
}
//7.视图已经消失
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
      NSLog(@"%s",__FUNCTION__);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

下面的代码是在SecondViewController.m 中关于视图的跳回的代码.

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 100, 100, 100);
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 10;
    [self.view addSubview:button];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];    
}

- (void)click:(UIButton *)button
{
    //点击返回到前一个页面
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI 03 UIViewController (视图控制器)的7个方法与视图的跳转

标签:

原文地址:http://blog.csdn.net/gao_zi/article/details/47282419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!