iOS 笔记二:Multiple MVCs 、UINavigationController 、UITabBarController

实现方式,使用:
1. UINavigationController
2. UITabBarController

Add new MVC to your storyboard:
1.Drag "View Controller" form Object Palette.
2.Create a subclass of UIViewController using New File munu item.
3.Set that subcalss as the class of your new Controller in the Attributs Inspector.

How to present this new MVC to the user?
1.UINavigationController.
2.UITabBarController.
3.Other mechnisims.

How does it work?
1.Encoles other MVCs.
2.Touches in one MVC "segue" to the other MVCs.

UINavigationController 的组件:
1.Navigation Bar (内容由嵌入到UINavigationController的MVC的navigationItem决定).
A. Title(默认情况下,title是嵌入的MVC的属性).
B. 嵌入的MVC的 navigationItem.rightBarButtonItems(是UIButtonItems数组NSArray).
C. 返回按钮(自动生成).
2.嵌入的MVC的toolbarItems属性(也是UIBarButtonitems数组NSArray).

3.我们把Xcode中的Controller中的一个视图(View)的布局称为场景(scene).
Navigation Bar作为UINavigationController的视图的组成部分,永远覆盖在场景的上方。

一个压入的MVC在什么时候会被弹出?
1.按下返回按钮(左上角).
2.编程调用UINavigationController的实例方法:
 -(void)popViewControllerAnimated:(BOOL)animated; 尽量少使用这个方法, 而是让用户控制。

注意:所有的UIViewControllers都知道它们所在的UINavigationController
如果它们不属于任何一个,那么self.navigationController是nil。

以代码方式运行一个segue:
点击按钮直接触发segue是合理的,但是有时候一些条件segue需要编程以代码方式
来触发:
-(void)performSegueWithIdentifier:(NSString *)segueId sender:(id)sender;
如下:
-(IBAction)rentEquipment
{
 if(self.snowTraversingTalent == Skiing) {
  [self performSegueWithIdentifier:@"AskAboutSkis" sender:self];
 } else {
  [self performSegueWithIdentifier:@"AskAboutSnowboard" sender:self];
 }
}



Segues
当一个segue发生时,会运行些什么代码?
segue给发起segue的VC提供准备新VC的机会。
以下方法将会发送给包含发起segue按钮的VC:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if([segue.identifier isEqualToString:@"DoSomething"]) {
  if([segue.destinationViewController isKindOfClass:[DoSomethingVC class]]) {
   DoSomethingVC * pVC = (DoSomethingVC *)segue.destinationViewController;
   pVC.needInfo = ...;
  }
 }
}
你应该把新VC需要的数据在这里传送给它,以便让它能正常的运行起来。


在storyboard中用名字来实例化一个UIViewController.
有时候你想要不使用segue来把一个VC展示到屏幕上,可以使用以下方法:
NSString *vcid = @"something";
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:vcid];
一般的,你可以在一个现存的UIViewController的self.storyboard域来获取storyboard;
标识符"vcid"必须是在Xcode中UIViewController设置的identifier。

Demo: 在一个target/action方法中创建一个UIViewController并把它显示到屏幕
在storyboard中布局好一个名为"doit1"的DoitViewController
-(IBAction)doit
{
 DoitViewController * doit = 
  [self.storyboard instantiateViewControllerWithIdentifier:@"doit1"];
 doit.infoDoitNeeds = self.info;
 [self.navigationController pushViewController:doit animated:YES];
}

注意这里又使用了self.navigationController.



UITabBarController
UITabBarController可以包含UINavigationController,反过来不行。
从对象面板中拖拽出一个Tab Bar Controller来创建它。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值