使用一个控制器A去管理多个控制器,如B,C;控制器A称为父控制器,B,C称为子控制器;
iOS提供了这两个比较特殊的控制器,根据需要可以自定义;
UINavigationController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// 创建窗口
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 窗口背景色
_window.backgroundColor = [UIColor whiteColor];
// 实例控制器 a
AViewController *a = [[AViewController alloc] init];
// 实例导航控制器 nav
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:a];
// 设置根控制器为nav
self.window.rootViewController = nav;
//
[_window makeKeyAndVisible];
return YES;
}
UITabBarController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// 创建窗口
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
// 窗口背景色
_window.backgroundColor = [UIColor whiteColor];
// 实例控制器 a
AViewController *a = [[AViewController alloc] init];
BViewController *b = [[BViewController alloc] init];
// 实例导航控制器 anav
UINavigationController *anav = [[UINavigationController alloc] initWithRootViewController:a];
UINavigationController *bnav = [[UINavigationController alloc] initWithRootViewController:b];
// 实例tab
UITabBarController *tab = [[UITabBarController alloc] init];
// nav加到tab上
tab.viewControllers = @[anav, bnav];
// 设置根控制器为nav
self.window.rootViewController = tab;
//
[_window makeKeyAndVisible];
return YES;
}