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-03-30 15:37:12

    PM2下使用 npm run 命令

    npm run xxxx 是 node常用的启动方式之一,本文介绍下如何用PM2来实现该方式的启动。 下面是项目的package.json文件部分代码:

  • 2020-04-01 10:15:11

    vue-cli3.0中vue.config.js的基本配置(去除esLint提示和解决build后静态文件路径报错)

    vue-cli是开发vue项目必不可少的脚手架工具,3.0版本之前的目录结构是由config目录的,关于webpack配置的文件都放在该目录里面,而3.0版本之后做了一个大更新,webpack相关的默认配置都帮你集成到webpack的npm 包里面去了,官方提供的一种修改配置的方式就是开发者自己在项目的根目录(与src同层级的目录)下建立一个vue.config.js的文件,然后去覆盖自定义的配置,达到你想要的效果,下面写一个基本的默认配置

  • 2020-04-01 10:21:20

    Vue extend $mount 构造器详解

    本节介绍两个 Vue.js 内置但却不常用的 API——extend 和 $mount,它们经常一起使用。不常用,是因为在业务开发中,基本没有它们的用武之地,但在独立组件开发时,在一些特定的场景它们是至关重要的。

  • 2020-04-01 15:36:52

    CSS3中的transition属性详解

    transition: property duration timing-function delay transition属性是个复合属性,她包括以下几个子属性: transition-property :规定设置过渡效果的css属性名称 transition-duration :规定完成过渡效果需要多少秒或毫秒 transition-timing-function :指定过渡函数,规定速度效果的速度曲线 transition-delay :指定开始出现的延迟时间