IOS 页面跳转方法总结

作者: 杰克大王 | 来源:发表于2016-06-16 15:18 被阅读650次

    尝试列出所有的页面跳转方法,有疏漏的欢迎补充。

    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 合用的例子

    Paste_Image.png
    
    
    //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);
    

    期待补充

    相关文章

      网友评论

      • wayne_YU:能留一个联系方式 交流一下吗
        杰克大王:@wayne_YU 简书留言就好,有空了我就会来看看,回复回复
      • 云画的跃光:如果a页面跳转到b页面,b不需要返回a页面,怎么办?我的b页面有一个leftBarButton,不是我定义的
        杰克大王:@馒头小爱 调用self presentViewController 方法
      • Dariel:但实际开发中页面跳转逻辑会很复杂,可以封装个库,通过自定义url作跳转
        杰克大王:@Dariel在杭州 对的,目前的项目中,封装了URLRouter进行跳转
      • 蕾蕾是女神:最后一段代码是什么意思?
        杰克大王:@蕾蕾是女神 UIViewController 有一个管理子controller的方法addChildViewController,添加后,可以调用transitionFromViewController 在子controller页面间跳转

      本文标题:IOS 页面跳转方法总结

      本文链接:https://www.haomeiwen.com/subject/tfemdttx.html