斯坦福大学iOS应用开发教程学习笔记(第六课)故事版 StoryBoard

来源:互联网 发布:大数据职称 编辑:程序博客网 时间:2024/06/05 23:48

第六课 主要内容:多个MVC的程序和故事版、UINavigationController、 Segues


1、多个MVC

前面的程序都是一个MVC,多个View时,怎么办,那就需要多个Controller。一个MVC只能控制一屏幕或更小的区域。

那如何切换两个MVC呢,用控制器群里的控制器:UINavigationController。


2、UINavigationController是个控制器

也是继承于UIVIewController。UINavigationController的长相如下图:


中间有个title。

它是个特殊的controller,因为它有一个Outlet只向一另外一个MVC,就是它的rootViewController。

rootViewController就是出现在白色区域的。原来的rootViewController放到UINavigationController后,它 的bounds高度会变小一些。

通过执行一个segues,可以跳转到另外一个MVC上。就是把新的MVC push都屏幕上,点返回,把当前的MVC pop出来。

3、segues

segues有三种方式:

push 

model

custom 

4、添加Navigation Controller

选中你要嵌入的view Controllser,然后通过Editor的 Embed in包含进来一个Navigation Controller。



这个箭头表示程序的开始。

 


5、pop一个ViewController的方法:

[cpp] view plaincopy
  1. - (void)popViewControllerAnimated:(BOOL)animated;  

6、两个关于segues非常重要的方法

跳转前准备的方法

[cpp] view plaincopy
  1. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  
  2. {  
  3. if ([segue.identifier isEqualToString:@“DoAParticularThing”]) {  
  4.     UIViewController *newController = segue.destinationViewController;  
  5. }  
  6. }  


可以决定你是否跳转到这个页面,获取到即将跳转页面的controller的实例,这样可以提前去设置它的属性。

通过Identifier跳转的方法:

[cpp] view plaincopy
  1. - (void)performSegueWithIdentifier:(NSString *)segueId sender:(id)sender;  
例子:
[cpp] view plaincopy
  1. - (IBAction)rentEquipment  
  2. {  
  3. if (self.snowTraversingTalent == Skiing) {  
  4. [self performSegueWithIdentifier:@“AskAboutSkis” sender:self];  
  5. else {  
  6. [self performSegueWithIdentifier:@“AskAboutSnowboard” sender:self];  
  7. }  
  8. }  

7、通过故事版来实例化一个ViewController的方法

[cpp] view plaincopy
  1. NSString *vcid = @“something”;  
  2. UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:vcid];  

self.storyboard是一个ViewController的属性。

例子:

[cpp] view plaincopy
  1. - (IBAction)doit  
  2. {  
  3. DoitViewController *doit =  
  4. [self.storyboard instantiateViewControllerWithIdentifier:@”doit1”];  
  5. doit.infoDoitNeeds = self.info;  
  6. [self.navigationController pushViewController:doit animated:YES];  
  7. }  
获取后,把它push到navigationController展示。

8、一个StoryBoard和Segues的Demo

主要内容:

  • 在两个viewController之间创建segue
  • 把ViewController内嵌到NavigationController中去。


segue有两个很重要的属性

这两个属性在跳转时经常用到

  • identifier 
  • destinationViewController

Demo源码下载地址:http://www.stanford.edu/class/cs193p/cgi-bin/drupal/downloads-2011-fall

容芳志 (http://blog.csdn.net/totogo2010)

本文遵循“署名-非商业用途-保持一致”创作公用协议


原文地址:http://blog.csdn.net/totogo2010/article/details/8226430

0 0
原创粉丝点击