尝试列出所有的页面跳转方法,有疏漏的欢迎补充。
1.UINavigationController - push & pop (常用)
在AppDelegate didFinishLaunchingWithOptions中指定首页面
//myView一个btn 点击后调用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中,可调用
[self.navigationController pushViewController:self.secondVC animated:YES];
来完成页面跳转
同样,在跳转完成后的页面中,可以调用
[self.navigationController popViewControllerAnimated:YES];
来回到上一个页面。
2.UITabBarController
微信的页面就是一个UITabBarController 和 NavigationController 合用的例子
//1.创建Window
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
//a.初始化一个tabBar控制器
UITabBarController *tb=[[UITabBarController alloc]init];
//设置控制器为Window的根控制器
self.window.rootViewController=tb;
//b.创建子控制器
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=@"联系人";
nc2.tabBarItem.image=[UIImage imageNamed:@"tab_buddy_nor"];
MyViewController *c3=[[MyViewController alloc]init];
UINavigationController *nc3 = [[UINavigationController alloc] initWithRootViewController:c3];
nc3.tabBarItem.title=@"动态";
nc3.tabBarItem.image=[UIImage imageNamed:@"tab_qworld_nor"];
MyViewController *c4=[[MyViewController alloc]init];
UINavigationController *nc4 = [[UINavigationController alloc] initWithRootViewController:c4];
nc4.tabBarItem.title=@"设置";
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.设置Window为主窗口并显示出来
[self.window makeKeyAndVisible];
3.(Modal)UIViewController - presentView
UIViewController或者其子类的方法中调用
[self presentViewController:self.secondVC animated:YES completion:nil];
默认从下至上弹出一个VC来展示。
[self dismissViewControllerAnimated:YES completion:nil];
关闭弹出的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);
期待补充
网友评论