嘗試列出所有的頁面跳轉(zhuǎn)方法,有疏漏的歡迎補充。
1.UINavigationController - push & pop (常用)
在AppDelegate didFinishLaunchingWithOptions中指定首頁面
//myView一個btn 點擊后調(diào)用push
UIViewController *myViewController = [[MyViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
在指定的myViewController中,可調(diào)用
[self.navigationController pushViewController:self.secondVC animated:YES];
來完成頁面跳轉(zhuǎn)
同樣宝踪,在跳轉(zhuǎn)完成后的頁面中蔼紧,可以調(diào)用
[self.navigationController popViewControllerAnimated:YES];
來回到上一個頁面俭嘁。
2.UITabBarController
微信的頁面就是一個UITabBarController 和 NavigationController 合用的例子
Paste_Image.png
//1.創(chuàng)建Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//a.初始化一個tabBar控制器
UITabBarController *tb=[[UITabBarController alloc]init];
//設(shè)置控制器為Window的根控制器
self.window.rootViewController=tb;
//b.創(chuàng)建子控制器
MyViewController *c1=[[MyViewController alloc]init];
UINavigationController *nc1 = [[UINavigationController alloc] initWithRootViewController:c1];
nc1.view.backgroundColor=[UIColor grayColor];
nc1.view.backgroundColor=[UIColor greenColor];
nc1.tabBarItem.title=@"消息";
nc1.tabBarItem.image=[UIImage imageNamed:@"tab_recent_nor"];
nc1.tabBarItem.badgeValue=@"123";
MyViewController *c2=[[MyViewController alloc]init];
UINavigationController *nc2 = [[UINavigationController alloc] initWithRootViewController:c2];
nc2.view.backgroundColor=[UIColor brownColor];
nc2.tabBarItem.title=@"聯(lián)系人";
nc2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
MyViewController *c3=[[MyViewController alloc]init];
UINavigationController *nc3 = [[UINavigationController alloc] initWithRootViewController:c3];
nc3.tabBarItem.title=@"動態(tài)";
nc3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
MyViewController *c4=[[MyViewController alloc]init];
UINavigationController *nc4 = [[UINavigationController alloc] initWithRootViewController:c4];
nc4.tabBarItem.title=@"設(shè)置";
nc4.tabBarItem.image=[UIImage imageNamed:@"tab_me_nor"];
//c.添加子控制器到ITabBarController中
//c.1第一種方式
//[tb addChildViewController:nc1];
//[tb addChildViewController:nc2];
//c.2第二種方式
tb.viewControllers=@[nc1,nc2,nc3,nc4];
//2.設(shè)置Window為主窗口并顯示出來
[self.window makeKeyAndVisible];
3.(Modal)UIViewController - presentView
UIViewController或者其子類的方法中調(diào)用
[self presentViewController:self.secondVC animated:YES completion:nil];
默認從下至上彈出一個VC來展示拨拓。
[self dismissViewControllerAnimated:YES completion:nil];
關(guān)閉彈出的VC
4.addChildViewController
@interface UIViewController (UIContainerViewControllerProtectedMethods)
// An array of children view controllers. This array does not include any presented view controllers.
@property(nonatomic,readonly) NSArray<__kindof UIViewController *> *childViewControllers NS_AVAILABLE_IOS(5_0);
/*
If the child controller has a different parent controller, it will first be removed from its current parent
by calling removeFromParentViewController. If this method is overridden then the super implementation must
be called.
*/
- (void)addChildViewController:(UIViewController *)childController NS_AVAILABLE_IOS(5_0);
/*
This method can be used to transition between sibling child view controllers.
*/
- (void)transitionFromViewController:(UIViewController *)fromViewController
toViewController:(UIViewController *)toViewController
duration:(NSTimeInterval)duration
options:(UIViewAnimationOptions)options
animations:(void (^ __nullable)(void))animations
completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(5_0);
期待補充