视图控制器


视图控制器UIViewController,视图控制,在新版本xcode中会默认生成一个ViewController。

1.新建UIViewController

右键->New File添加一个继承自UIViewController的类View1Controller,在viewDidLoad中添加button,并设置button点击事件为关闭此ViewController

self.view.backgroundColor=[UIColor redColor];
    
    UIButton *closeViewBtn=[[UIButton alloc] init];
    closeViewBtn.backgroundColor=[UIColor blueColor];
    closeViewBtn.frame=CGRectMake(10, 30, 100, 30);
    [closeViewBtn setTitle:@"Close" forState:UIControlStateNormal];
    [closeViewBtn addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:closeViewBtn];

点击事件

-(void) closeView
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

2.在ViewController中添加一个button用来控制View1Controller的启动出现

//ViewController
    UIButton *showView1Btn=[[UIButton alloc] init];
    showView1Btn.frame=CGRectMake(10, 470, 100, 30);
    showView1Btn.backgroundColor=[UIColor redColor];
    [showView1Btn addTarget:self action:@selector(showView1:) forControlEvents:UIControlEventTouchDown];
    
    [self.view addSubview:showView1Btn];

3.视图的生命周期

在view1Controller中添加如下代码,在其启动过程中可以看到除viewDidLoad外其他方法的触发以及启动顺序

-(void) loadView
{
    [super loadView];
    NSLog(@"Load view");
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"View will appear");
}

-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"View did appear");
}

-(void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    NSLog(@"View will disappear");
}

-(void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"View did disappear");
}

-(void) closeView
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

4.其他弹窗方式

[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController: view1 animated: YES completion:nil];

[[UIApplication sharedApplication].keyWindow.rootViewController dismissViewControllerAnimated:YES completion:^{}];

在unity生成的工程中可以通过GetAppController().rootViewController获取到根视图,既而进行试图操作,一般插件中用此方法较多

[GetAppController().rootViewController presentViewController: view1 animated:YES completion:NULL];
[GetAppController().rootViewController dismissViewControllerAnimated:YES completion:NULL];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值