ios7.0结合storyborad实现页面跳转的总结

折腾了一成天,本文总结一下ios7.0页面跳转有关的内容javascript

storyboard的潜规则

我接触ios很晚,环境已是xcode5+ios7,因此对之前的IOS开发模式并不了解。在网上查阅了不少资料,发现之前的代码,不少都须要本身coding来建立ViewController,好比:html

 

Objc代码   收藏代码
  1. WTwoViewController *controller = [[WTwoViewController alloc]initWithNibName:@"WTwoViewController" bundle:nil];  
  2. [self presentViewController:controller animated:YES completion:nil];  


可是用storyboard来管理view controller的话,storyboard会自动处理view controller的初始化动做,因此就再也不须要本身coding来建立view controller的实例。在另一篇博客里看到这句话:java

 

用过xib的人我相信不少人都会常常用到-presentModalViewController:animated:以及-pushViewController:animated:这两个方法。这种代码在storyboard里将成为历史;取而代之的是Segue”ios

基于控件的跳转

用storyboard作开发,常常须要拉线,本文不介绍,请看这篇官方文档:web

start developing iOS app todaybootstrap

这种拉线,是从button拉到view controller:api

这种方式只要点击了这个button,就会自动跳转,不须要写任何代码xcode

 

直接从controller到controller

 
这种拉线是直接从View Controller到View Controller:
 
这种方式已经预先建立了segue,可是还须要手工编码,首先须要给segue设置一个identity
 
而后写代码来跳转:
Objc代码   收藏代码
  1. // 跳转到bootstrap  
  2. - (void) jumpToBootstrap{  
  3.     [self performSegueWithIdentifier:@"fromWelcomeToBootstrap" sender:self];  
  4. }  
这段代码必须写在-viewDidAppear里,不能写在-viewDidLoad里,不然会报一个错误:whose view is not in window hierarchy!
 

页面之间传值

 
之前用-presentModalViewController:animated:方法来跳转的时候,通常须要经过delegate等方式来传值,如今一概用segue API就能搞定
 
在segue发生以前,先会调用当前View Controller的-prepareForSegue:sender:方法,能够在里面作一些处理,好比:
Objc代码   收藏代码
  1. BootstrapViewController* targetController = [segue destinationViewController];// 拿到目标view controller,而后要怎么样均可以了  
不过这里要注意的是,彷佛不能在prepareForSegue方法里设置destination view controller的view,由于这个时候view尚未被storyboard实例化。不过能够先传参,后面再设置
 
另外网上看到不少帖子,都说从B回到A的时候若是也须要传值,能够把A设置成B的delegate:
Objc代码   收藏代码
  1. BViewController.delegate = self;  
这里我不是很理解,当从B回到A的时候,也设置一个segue彷佛就好了
 

unwind segue

 
unwind segue比较特殊,是在目标View Controller里先设置一个action,而后在source View Controller里拖线到exit图标上。这种状况下,除了会调用source的prepareForSegue方法之外,target View Controller的那个action也会被调用。
相关文章
相关标签/搜索