The First:StoryBoard导航
思想:
在Xcode中,程序运行顺序如下:Xib —> 代码 —> StoryBoard(后者亲测,前者未测)
细分:
当AppDelegate没有程序入口时,StoryBord有入口时,系统从StoryBord中进入
当StoryBord和AppDelegate都有入口时,优先从AppDelegate进入
当既没有AppDelegate也没有StoryBrod,那么程序从默认的ViewController开始
第一种:Root+ViewControllers
思想:
self.window.rootViewController=_tabbar;
_tabbar.viewControllers=@[nav1, nav2];
图一:
图二:
全部代码:
#import"AppDelegate.h"
#import"ViewController.h"
#import"HomeViewController.h"
@interfaceAppDelegate()
@property(nonatomic,strong)UITabBarController*tabbar;
@property(nonatomic,strong)UINavigationController*NC;
@end
@implementationAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//1、初始化Window
self.window=[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
//2、初始化NV
ViewController*VC = [[ViewControlleralloc]init];
VC.title=@"diyige";
VC.view.backgroundColor= [UIColorredColor];
HomeViewController*HomeVC = [[HomeViewControlleralloc]init];
HomeVC.title=@"dierge";
HomeVC.view.backgroundColor= [UIColorgrayColor];
UINavigationController*nav1 =[[UINavigationControlleralloc]initWithRootViewController:VC];
UINavigationController*nav2 =[[UINavigationControlleralloc]initWithRootViewController:HomeVC];
//3、UINavigationController——> UITabbarController
_tabbar=[[UITabBarControlleralloc]init];
_tabbar.viewControllers=@[nav1, nav2];
_tabbar.delegate=self;
//5、UITabBarController ——> Window
self.window.rootViewController=_tabbar;
//6、Window ——> makeKeyVisible
[self.windowmakeKeyAndVisible];
//7、添加默认状态
[selftabBarController:_tabbardidSelectViewController:nav1];
returnYES;
}
//UITabBarControllerDelegate协议
-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController{
if(0 == tabBarController.selectedIndex) {
viewController.title=@"dudu";
tabBarController.navigationItem.rightBarButtonItem=nil;
}
else{
viewController.title=@"xixi";
tabBarController.navigationItem.rightBarButtonItem=nil;
}
}
第二种:全部Root
思想:
self.window.rootViewController=_NC;
_NC=[[UINavigationControlleralloc]initWithRootViewController:_tabbar];
图一:
图二:
全部代码:
#import"AppDelegate.h"
#import"ViewController.h"
#import"HomeViewController.h"
@interfaceAppDelegate()
@property(nonatomic,strong)UITabBarController*tabbar;
@property(nonatomic,strong)UINavigationController*NC;
@end
@implementationAppDelegate
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//1、初始化Window
self.window=[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds];
//2、初始化NV
ViewController*p1 = [[ViewControlleralloc]init];
HomeViewController*p2 = [[HomeViewControlleralloc]init];
p1.tabBarItem.title=@"p1";
p2.tabBarItem.title=@"p2";
//3、ViewController ——> UITabbarController
_tabbar=[[UITabBarControlleralloc]init];
_tabbar.viewControllers=@[p1,p2];
_tabbar.delegate=self;
//4、UINanigationController ——> UITabBarController
_NC=[[UINavigationControlleralloc]initWithRootViewController:_tabbar];
//5、UITabBarController ——> Window
self.window.rootViewController=_NC;
//6、Window ——> makeKeyVisible
[self.windowmakeKeyAndVisible];
//7、添加默认状态
[selftabBarController:_tabbardidSelectViewController:p1];
returnYES;
}
//UITabBarControllerDelegate协议
-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController{
if(0 == tabBarController.selectedIndex) {
tabBarController.title=@"dudu";
tabBarController.navigationItem.rightBarButtonItem=nil;
}
else{
tabBarController.title=@"xixi";
tabBarController.navigationItem.rightBarButtonItem=nil;
}
}
技巧:需要不止一个ViewController的请 command+c/v —> ViewController
提示:下次编辑 —> UILayoutContainerView / Xcode程序运行底层设计 / 两种导航点评