一 多控制器
1)一个iOS的app很少只由一个控制器组成,除非这个app极其简单
2)当app中有多个控制器的时候,我们就需要对这些控制器进行管理
3)有多个view时,可以用一个大的view去管理1个或者多个小view
4)控制器也是如此,用1个控制器去管理其他多个控制器
比如,用一个控制器A去管理3个控制器B、C、D 控制器A被称为控制器B、C、D的“父控制器” 控制器B、C、D的被称为控制器A的“子控制器”
举个例子:
首先设置加载面板为空,好让程序加载自定义控制器。
然后更新 AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建window
self.window = [[UIWindow alloc]init];
//给wiindow一个根控制器
UINavigationController *nav = [[UINavigationController alloc]init];
self.window.rootViewController = nav;
//设置主要的并且显示
[self.window makeKeyAndVisible];
return YES;
}
二 导航控制器
利用UINavigationController,可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是系统自带的“设置”应用。
UINavigationController以栈的形式保存子控制器
使用push方法能将某个控制器压入栈
@property(nonatomic,copy) NSArray *viewControllers; @property(nonatomic,readonly) NSArray *childViewControllers;
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
使用pop方法可以移除控制器
AppDelegate.m
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 3 self.window = [[UIWindow alloc]init]; 4 5 6 UINavigationController * nav = [[UINavigationController alloc]init]; 7 self.window.rootViewController = nav; 8 NSLog(@"%@" , nav.navigationBar ); 9 10 UIViewController *vc1 =[[UIViewController alloc]init]; 11 vc1.view.backgroundColor = [UIColor redColor]; 12 13 UIViewController *vc2 =[[UIViewController alloc]init]; 14 vc2.view.backgroundColor = [UIColor orangeColor]; 15 16 17 [nav pushViewController:vc1 animated:NO]; 18 [nav pushViewController:vc2 animated:NO]; 19 20 [self.window makeKeyAndVisible]; 21 22 return YES; 23 }
也可以通过这中方式来生成多个导航控制器:
AppDelegate.m
1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 3 self.window = [[UIWindow alloc]init]; 4 5 6 UINavigationController * nav = [[UINavigationController alloc]init]; 7 self.window.rootViewController = nav; 8 NSLog(@"%@" , nav.navigationBar ); 9 10 UIViewController *vc1 =[[UIViewController alloc]init]; 11 vc1.view.backgroundColor = [UIColor redColor]; 12 13 UIViewController *vc2 =[[UIViewController alloc]init]; 14 vc2.view.backgroundColor = [UIColor orangeColor]; 15 16 nav.viewControllers = @[vc1,vc2]; 17 18 [self.window makeKeyAndVisible]; 19 return YES; 20 }
也可以通过UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc1];来直接生成根控制器。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc]init]; UIViewController *vc1 =[[UIViewController alloc]init]; vc1.view.backgroundColor = [UIColor redColor]; UIViewController *vc2 =[[UIViewController alloc]init]; vc2.view.backgroundColor = [UIColor orangeColor]; UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc1]; [nav pushViewController:vc2 animated:YES]; self.window.rootViewController = nav; [self.window makeKeyAndVisible]; return YES; }
设置导航按钮的标题:
self.navigationItem.title = @"One";
设置导航的返回值:
如果要设置第二个页面的返回按钮,那么需要在第一个页面设置。
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
设置左边的的按钮
self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"left" style:UIBarButtonItemStylePlain target:nil action:nil];
删除栈顶控制器,跳转回上一个控制器
UINavigationController * nav = [self navigationController];
[nav popViewControllerAnimated:YES];
知识点:
Xcode: commond + option+ 回车 在storyboard/xib中打开代码区域。