iOS ViewController的四种跳转方法

2021-01-12 22:04:56

ViewController 跳转方法总结

参考地址 iOS ViewController的四种创建方法

要实现的功能:从一个VC中点击Button跳转到另一个VC

首先将第一个视图里button的点击方法拖到ViewController.m文件中

- (IBAction)ClickBtn:(id)sender {}

一.StoryBoard里面获取ViewController

1.在StoryBoard里创建一个VC,并设置它的StoryBoard ID

2.创建VC的文件,继承自UIViewController,为了命名方便,我这里直接命名为ViewController1

3.在ViewController.m

- (IBAction)ClickBtn:(id)sender {//获取当前的StoryboardUIStoryboard*sb=[UIStoryboard storyboardWithName:@"Main" bundle:nil];//在Storyboard中获取vc1ViewController1*vc1=[sb instantiateViewControllerWithIdentifier:@"vc1"];//呈现出vc1[self presentViewController:vc1 animated:YES completion:^{}];}

二.Xib文件初始化

1.创建VC的文件,继承自UIViewController,为了命名方便,我这里直接命名为ViewController2

2.在Xib中

3.在ViewController.m

- (IBAction)ClickBtn:(id)sender {//initWithNibName后+vc2文件名ViewController2*vc2=[[ViewController2 alloc]initWithNibName:@"ViewController2" bundle:nil];//呈现出vc2[self presentViewController:vc2 animated:YES completion:^{}];}

三.纯代码创建

1.创建VC的文件,继承自UIViewController,为了命名方便,我这里直接命名为ViewController3

2.代码设置背景色,创建button

self.view.backgroundColor=[UIColor whiteColor];UIButton*btn=[UIButton buttonWithType:UIButtonTypeCustom];btn.frame=CGRectMake(100, 100, 100, 100);[btn setTitle:@"出去" forState: UIControlStateNormal];[btn addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];btn.backgroundColor=[UIColor blueColor];[self.view addSubview:btn];

3.在ViewController.m

- (IBAction)ClickBtn:(id)sender {ViewController3*vc3=[[ViewController3 alloc]init];//呈现出vc3[self presentViewController:vc3 animated:YES completion:^{}];}

四.可视化操作

五.从第二个视图回到第一个视图操作很简单,仅在第二个VC的button点击事件中加上这样一句代码

[self dismissViewControllerAnimated:YES completion:^{NSLog(@"vc跳出");}];


  • 2018-01-18 11:26:53

    浅谈js运行机制(线程)

    从开始接触js时,我们便知道js是单线程的。单线程,异步,同步,互调,阻塞等。在实际写js的时候,我们都会用到ajax,不管是原生的实现,还是借助jQuery等工具库实现,我们都知道,ajax可以实现局部刷新,并且在请求处理时,任然可以响应用户的操作,比如点击事件。不是说js是单线程吗?这些都是怎么实现的? 在阅读《深入理解Bootrap的源码》一书,在分析轮播组件(carousel.js)的源码时,作者对一句代码操作的注释引起了我的兴趣。

  • 2018-01-18 17:41:05

    Fatal error: Class 'Memcached' not found

    从别地方复制来了新的配置文件,包了这样的错误,因为我也是刚刚安装了php7,以为是php的问题,于是就根据网上的提示,安装memcached.dll包。

  • 2018-01-20 20:33:58

    PHP中session_cache_limiter() 是什么意思啊

    session_cache_limiter('public'); session_cache_expire(30); session_start();第三个我晓得 前两个 网络上没找着资料 手册上全是英文的 翻译后 还是看不懂主要是没翻译正确