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-12-07 08:26:37

    mysql线程池和连接池的区别

    可能有的DBA会把线程池和连接池混淆,其实两者是有很大区别的,连接池一般在客户端设置,而线程池是在DB服务器上配置;另外连接池可以取到避免了连接频繁创建和销毁,但是无法取到控制MySQL活动线程数的目标,在高并发场景下,无法取到保护DB的作用。比较好的方式是将连接池和线程池结合起来使用。 作者:飞鸿无痕 链接:https://www.jianshu.com/p/88e606eca2a5 來源:简书 简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 2018-12-07 17:47:24

    linux中wc命令用法

    Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

  • 2018-12-07 22:19:33

    修改 Nginx 进程最大可打开文件数(worker_processes和worker_connections)

    worker_processes:操作系统启动多少个工作进程运行Nginx。注意是工作进程,不是有多少个nginx工程。在Nginx运行的时候,会启动两种进程,一种是主进程master process;一种是工作进程worker process。例如我在配置文件中将worker_processes设置为4,启动Nginx后,使用进程查看命令观察名字叫做nginx的进程信息,我会看到如下结果:

  • 2018-12-07 22:55:02

    nginx worker_processes 配置

    据另一种说法是,nginx开启太多的进程,会影响主进程调度,所以占用的cpu会增高, 这个说法我个人没有证实,估计他们是开了一两百个进程来对比的吧。