iOS开发技巧iOS dev

iOS界面间的跳转思考

2016-05-04  本文已影响2881人  specter_hhg

本文基于OC语言,浅浅探索一下iOS几种跳转方式。先看一下本人做的思维导图。

iOS界面几种跳转方式

iOS分可视化开发(storyboard和xib)和代码开发两种方式。当我们只使用某一种开发方式进行开发的时候,界面跳转可能不会遇到什么问题。但是当我们混合开发的时候,界面间的跳转有时候会遇到一些莫名其妙的问题。而实际开发项目中,经常因实际需求混合各种开发方式。因此,本人罗列了一下界面间跳转可能遇到的九种情况。经过测试,实际上有三种跳转的写法。分别是:

1.xxx界面向storyboard界面跳转

2.xxx界面向xib界面跳转

3.xxx界面向代码界面跳转

现在,我们来一一测试一下。xxx界面向storyboard界面跳转。先创建一个工程,如图:

创建工程

然后再新建一个storyboard

新建storyboard

storyboard创建完是空白的

storyboard创建之后的样子

我们要拖一个UIViewcontroller进storyboard内

往内拖

拖完之后的界面我们就比较熟悉了

拖完UIViewcontroller的样子

再创建一个UIVIewController的类

创建第二个storyboard关联的VC

选中故事板的类,把刚刚创建的类和storyboard的UIVIewcontroller关联起来

关联storyboard和VC

再顺便写一下storyboard的ID,等下会用到

storyboard的VC的ID

关联完之后我们回到第一个UIViewController写跳转方法,为了方便展示跳转效果,我们写个btn

创建按钮

//故事板跳故事板

UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

[self.viewaddSubview:btn];

btn.backgroundColor= [UIColorredColor];

[btnaddTarget:selfaction:@selector(btnClick)forControlEvents:UIControlEventTouchDragInside];

btn.frame=CGRectMake(100,100,100,100);

接下来我们在btn里面写跳转方法

写跳转方法

//故事板--跳到--》故事板

- (void)btnClick

{

UIStoryboard*myStoryboard = [UIStoryboardstoryboardWithName:@"Second"bundle:nil];

UIViewController*secondVC = [myStoryboard  instantiateViewControllerWithIdentifier:@"secondViewController"];

[selfpresentViewController:secondVCanimated:YEScompletion:nil];

NSLog(@"跳转");

}

这样就完成了跳转,我们运行测试一下试试

运行模拟器 点击按钮实现跳转

这样我们就实现了xxx界面向storyboard界面跳转。下面我们来看看另一种跳转方式 xxx界面向xib界面跳转

首先,我们先创建一个xib界面

xib文件打上勾

创建完,我们开始来准备跳转,跳转之前给xib设置一个背景色,方便区分

设置背景颜色

好了,回到最初的ViewController来实现跳转到xib文件去,首先,导入xib那个类的头文件,还有再写个按钮来实现跳转

导入头文件,创建btn

#import"XIBViewController.h"

//故事板跳xib

UIButton*btnXIB = [UIButtonbuttonWithType:UIButtonTypeCustom];

[self.viewaddSubview:btnXIB];

btnXIB.backgroundColor= [UIColorblueColor];

[btnXIBaddTarget:selfaction:@selector(btnXIBClick)forControlEvents:UIControlEventTouchDragInside];

btnXIB.frame=CGRectMake(100,210,100,100);

来实现跳转一下

编写btn的点击方法

//故事板--跳到--》xib

- (void)btnXIBClick

{

XIBViewController*xibVC=[[XIBViewControlleralloc]initWithNibName:@"XIBViewController"bundle:nil];

[selfpresentViewController:xibVCanimated:YEScompletion:nil];

}

我们来运行测试一下效果。

运行效果

蓝色按钮是跳转到xib界面的,我们点击一下看看

实现跳转到xib界面

点击后实现跳转。我们这样就实现了xxx界面向xib界面跳转。最后我们再来看看最普遍的跳转方式 xxx界面向代码界面跳转。

同样的,先创建一个UIViewController,我们修改一下它的背景颜色为绿色,然后进行跳转

创建一个纯代码界面

self.view.backgroundColor=[UIColorgreenColor];

回到UIViewController来写跳转方式

编写跳转方法

#import"CodeViewController.h"

//故事板跳code

UIButton*btnCode = [UIButtonbuttonWithType:UIButtonTypeCustom];

[self.viewaddSubview:btnCode];

btnCode.backgroundColor= [UIColorpurpleColor];

[btnCodeaddTarget:selfaction:@selector(btnCodeClick)forControlEvents:UIControlEventTouchDragInside];

btnCode.frame=CGRectMake(100,320,100,100);

//故事板--跳到--》code

- (void)btnCodeClick

{

CodeViewController*codeVC = [[CodeViewControlleralloc]init];

[selfpresentViewController:codeVCanimated:YEScompletion:nil];

}

运行测试一下

运行效果

点击跳转按钮

点击按钮跳转成功

成功实现了xxx界面向代码界面跳转。以上就是iOS界面的几种不同开发界面之间的跳转方式。希望对看到此博客的人有所帮助。

上一篇下一篇

猜你喜欢

热点阅读