您的位置:首页 > 移动开发 > IOS开发

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

2013-11-07 22:07 801 查看

第六课 主要内容:多个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

- (void)popViewControllerAnimated:(BOOL)animated;  

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

跳转前准备的方法

[cpp]
view plaincopy

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender  
{  
if ([segue.identifier isEqualToString:@“DoAParticularThing”]) {  
    UIViewController *newController = segue.destinationViewController;  
}  
}  

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

通过Identifier跳转的方法:

[cpp]
view plaincopy

- (void)performSegueWithIdentifier:(NSString *)segueId sender:(id)sender;  

例子:

[cpp]
view plaincopy

- (IBAction)rentEquipment  
{  
if (self.snowTraversingTalent == Skiing) {  
[self performSegueWithIdentifier:@“AskAboutSkis” sender:self];  
} else {  
[self performSegueWithIdentifier:@“AskAboutSnowboard” sender:self];  
}  
}  

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

[cpp]
view plaincopy

NSString *vcid = @“something”;  
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:vcid];  

self.storyboard是一个ViewController的属性。

例子:

[cpp]
view plaincopy

- (IBAction)doit  
{  
DoitViewController *doit =  
[self.storyboard instantiateViewControllerWithIdentifier:@”doit1”];  
doit.infoDoitNeeds = self.info;  
[self.navigationController pushViewController:doit animated:YES];  
}  

获取后,把它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)

本文遵循“署名-非商业用途-保持一致”创作公用协议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐