zoukankan      html  css  js  c++  java
  • ios视图切换之push与present混用

    在变成过程中,经常遇到两个视图控制器之间的切换,导航控制器即UINaVigation是最常用的一种,有时为了某些效果又需要进行模态切换,即present。

    我们的布局经常是在window上加一个nav,然后以viewControl作为nav的根视图进行导航。如果在导航之间有了一个present之后,你会发现当前页面的navigationController是空的,也就是说导航控制器不管用了抓狂,该咋弄呢睡觉

    下面就给大家介绍两种比较有效的方法:

    第一:在进行present之前,重新生成一个导航控制器,然后将下一个视图作为新生成的导航控制器的跟视图,将导航控制器present就行了睡觉看代码:

        ThirdViewController *thirdCtr=[[ThirdViewController alloc]init];
        
        UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:thirdCtr];
        
        [self presentViewController:nav animated:YES completion:nil];
        [thirdCtr release];
        [nav release];
    

    这样的话问题基本解决了,但就是没有回到最初的跟视图,只能在当前的导航控制器之间切换。

    第二种方法就比较好了,获取当前的window实例,在得到widow的跟视图,即为导航器,然后根据导航器的索引就可以找到当前的视图啦,睡觉睡觉睡觉

      FourthViewController *fourth=[[FourthViewController alloc]init];
        UIWindow *window=[[UIApplication sharedApplication]keyWindow];
        UINavigationController *nav0=(UINavigationController *)window.rootViewController;
        UIViewController *viewController=[nav0.viewControllers objectAtIndex:1];
        [viewController.navigationController pushViewController:fourth animated:YES];

    这样所有的视图都可以根据在导航控制器的层级关系找到了 安静可以啦 生气

    在查资料过程中又有一种新的收获,就是用push实现present的效果,代码就在下面了

    FiveViewController *fiveCtr=[[FiveViewController alloc]init];
        [self.navigationController pushViewController:fiveCtr animated:NO];
        
        UIViewController*cont =[[UIViewController alloc] initWithNibName:nil bundle:nil];
        [self presentModalViewController:cont animated:NO];
        [cont dismissModalViewControllerAnimated:NO];
    

    不管你信不信,他就是出现啦大笑大笑大笑

    demo下载demo下载

  • 相关阅读:
    (转载)Bonding技术指南
    Linux配置虚拟地址
    VB6之写注册表
    Tomcat集群搭建
    VBS连接远程Oracle
    机器学习 目标函数,损失函数
    深度学习理解内容 初
    leetcode 39. Combination Sum
    leetcode 33. Search in Rotated Sorted Array
    leetcode 29. Divide Two Integers
  • 原文地址:https://www.cnblogs.com/aukle/p/3217683.html
Copyright © 2011-2022 走看看