美文网首页
[转场动画] Push,Pop和Present,Dismiss

[转场动画] Push,Pop和Present,Dismiss

作者: wo一人两袖清风 | 来源:发表于2017-07-25 15:52 被阅读0次

    for (UIViewController *temp in self.navigationController.viewControllers) {

    if ([temp isKindOfClass:[你要跳转到的Controller class]]) {

    [self.navigationController popToViewController:temp animated:YES];

    }

    }

    1、[self.view addSubView:view];和[self.window addSubView:view];需要注意,此方法只是把页面(view)加在当前页面(view)上,控制器(viewController)还是原来那个控制器。此时再用[self.navigationColler pushViewController:animated:];和 [self.navigationController popViewControllerAnimated:]; 是不行的。要想使用pushViewController和popViewController进行视图间的切换,就必须要求当前视图是个NavigationController。

    2、有NavigationController导航栏的话,使用[self.navigationColler pushViewController:animated:];和[self.navigationController popViewControllerAnimated:];来进行视图切换。pushViewController是进入到下一个视图,popViewController是返回到上一视图。

    3、没有NavigationController导航栏的话,使用[self presentViewController:animated:completion:];和[self dismissViewControllerAnimated:completion:];具体是使用可以从文档中详细了解。

    4、要想使用pushViewController和popViewController来进行视图切换,首先要确保根视图是NavigationController,不然是不可以用的。这里提供一个简单的方法让该视图或者根视图是NavigationController。自己定义个子类继承UINavigationController,然后将要展现的视图包装到这个子类中,这样就可以使NavigationController了。提供的这个方法有很好的好处,就是可以统一的控制各个视图的屏幕旋转。将一个控制器(UIViewController)包装成一个导航控制器(UINavigationController):

    UIViewController *vc = [[UIViewController alloc] init];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];

    1. 用UINavigationController的时候用

    ----进入下一个视图[self.navigationColler pushViewController:animated:];

    ----返回之前的视图[self.navigationController popViewControllerAnimated:];

    ----ps:push以后会在navigation的left bar自动添加back按钮,它的响应方法就是返回。所以一般不需要写返回方法,点back按钮即可。

    2. 其他时候(视图不是UINavigationController的时候,只是一个viewController时)

    ----进入下一个视图:[self presentViewController:animated:completion:];

    ----返回之前的视图:[self dismissViewControllerAnimated:completion:];

    3. 切换视图一般用不到addSubview

    UINavigationController是导航控制器,如果pushViewController的话,会跳转到下一个ViewController,点返回会回到现在这个ViewController;如果是addSubview的话,其实还是对当前的ViewController操作,只是在当前视图上面又“盖”住了一层视图,其实原来的画面在下面呢,看不到而已。(当然,也可以用insertSubView  atIndex那个方法设置放置的层次)。

    相关文章

      网友评论

          本文标题:[转场动画] Push,Pop和Present,Dismiss

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