UI视图控制器的使用

1.新建视图控制器

2.在AppDelegate中添加

//视图控制器的使用
    MainViewController *mainVC = [[MainViewController alloc] init];

    // 把一个视图控制器指定为window的根视图控制器
    self.window.rootViewController = mainVC;
    
    // 内存管理
    [mainVC release];

3.在ViewControl.m中

// 加载视图    系统自己调用
- (void)loadView
{
    [super loadView];
    // 系统调用这个方法产生一个view, 作为自己自带的视图
    NSLog(@"%s", __func__);
}

// 视图已加载
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%s", __func__);
    // Do any additional setup after loading the view.
    
    // 控制器自带的视频被创建完毕就会调用这个方法
    // 视图创建的代码写在这个地方
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"suibian" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor yellowColor];
    button1.frame = CGRectMake(20, 20, 280, 50);
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    self.view.backgroundColor = [UIColor blackColor];
    
    // 视图控制器的生命周期
    
}

系统方法:

// 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%s", __func__);
}

// 视图已经出现
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%s", __func__);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    // 当app的内存过高,系统会调用这个方法
    // 可以在这个方法中释放掉一些可以重复生成的对象(数据/视图。。)
    NSLog(@"内存泄露啦啦啦啦");
}

推出一个新页面(新的视图控制器)

- (void)buttonClicked:(UIButton *)button
{
    NSLog(@"点击");
    
    // 模态推出
    
    // 推出一个新的页面(viewController)
    
    // 1, 创建新页面
    SecondViewController *secongVC = [[SecondViewController alloc] init];
    
    // 设置推出的动画效果
//    UIModalTransitionStyleCoverVertical,      //垂直翻页
//    UIModalTransitionStyleFlipHorizontal,     //水平翻转
//    UIModalTransitionStyleCrossDissolve,      //溶解消失
//    UIModalTransitionStylePartialCurl,        //卷曲翻页
    [secongVC setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [secongVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    [secongVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [secongVC setModalTransitionStyle:UIModalTransitionStylePartialCurl];

    
    // 2, 推出
    // 参数1,要推出的新视图控制器(viewController)
    // 参数2,是不是带一个动画效果
    [self presentViewController:secongVC animated:YES completion:^{
        
        // 推出新视图之后,要执行的代码
        
    }];
    
    // 3, 内存管理
    [secongVC release];
    
}

返回上一层页面

- (void)buttonClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值