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跳出");}];


  • 2020-04-17 14:06:29

    图片解释EaseIn,EaseOut,EaseInOut

    1.EaseIn:即缓动发生在入口处,也就是刚开始的时候。 2.EaseOut:即缓动发生在出口处,也就是结束之前。 3.EaseInOut:就是两边都有缓动了.

  • 2020-04-21 14:47:13

    Redis危险命令重命名、禁用

    flushdb,清空数据库 flushall,清空所有记录,数据库 config,客户端连接后可配置服务器 keys,客户端连接后可查看所有存在的键

  • 2020-04-21 15:13:15

    redis 简单使用

    Redis和Memcached类似,也属于k-v数据存储 Redis官网 https://redis.io支持更多value类型,除了和string外,还支持hash、lists(链表)、sets(集合)和sorted sets(有序集合) Redis是可以把数据存储在磁盘上的并且使用了两种文件格式:全量数据(RDB)和增量请求(aof)。一般叫做redis持久化 全量数据格式是把内存中的数据写入磁盘,便于下次读取文件进行加载。

  • 2020-04-21 15:14:20

    SpringBoot + Redis:基本配置及使用

    # Redis数据库索引(默认为0) spring.redis.database=0# Redis服务器地址 spring.redis.host=127.0.0.1# Redis服务器连接端口 spring.redis.port=6379# Redis服务器连接密码(默认为空) spring.redis.password=# 连接池最大连接数(使用负值表示没有限制) spring.redis.jedis.pool.max-active=20# 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.jedis.pool.max-wait=-1# 连接池中的最大空闲连接 spring.redis.jedis.pool.max-idle=10# 连接池中的最小空闲连接 spring.redis.jedis.pool.min-idle=0# 连接超时时间(毫秒) spring.redis.timeout=1000

  • 2020-04-22 10:49:10

    css按钮特效大全

    打开这个连接,你会发现很多按钮动画脚本,基本这些动画就够大家用的了。

  • 2020-04-27 09:26:49

    transform与transition区别与详解

    对元素进行移动、缩放、转动、拉长或拉伸。 方法:translate(): 元素从其当前位置移动,根据给定的 left(x 坐标) 和 top(y 坐标) 位置参数 有两个div,它们的css样式如下,transition:过渡 元素从一种样式逐渐改变为另一种的效果 有一个div,它的css样式如下: