IOS 使用presentViewController方法跳转的navigationController如何获取及视图层级关系
2018-03-22 11:21
603 查看
背景故事
工作中遇到一种应用场景。 在只能拿到tabBarController的情况下,需要在app内任何页面 跳转至新页面。为什么会遇到问题
因为没办法获取到当前视图的UINavigationController,首先确定用的跳转方法是(void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
遇到的问题:如果当前的页面是present过来的,就无法用tabBarController present至新页面,在不知道UIViewController有一个presentedViewController属性的前提。想通过[UIApplication sharedApplication].keyWindow.rootViewController 拿,发现present不会改变rootViewController。这就纳闷了,自然去想着弄清present过去的viewcontroller 在视图层级中是如何存在。
UITransitionView 的解释 :It’s difficult to say exactly what Apple will or won’t do however, adding a view to an undocumented view you retrieve from a superView message is not contentious. What you should not do (or be careful if you do do) is make assumptions about the view you are adding to. Specifically its class but even basic things like the fact that it even exists.
What are you trying to do? There may be a simpler way - like adding your view directly to the app’s UIWindow.
UITransitionView 是一个不公开的类,在保存controller 方面的作用应该和UINavigationController类似。 这也就是为什么present后 uiwindow.rootController 没有改变的原因。 因为它是被UITransitionView持有而不是uiwindow直接持有。
解决方法
其实uiviewcontroller是有presentedViewController这么一个属性,可以用一个while 循环直接拿到最后一层的present.第2张图中,可以作为跳转的viewcontroller 有三个 MainTabBarController、NavigationController以及最后一个。这三个 controller 用present方法 其实效果是一致的。uiwindow.rootController,下所有的controller 使用present 方法 都等于 rootController使用, 获取presentedViewController都是获取rootController的presentedViewController。
总结经验
其实苹果设计ios这么一个系统的时候,整体思维都是类似。 uiviewcontroller竟然有present这个方法,在这个方法中,有传入的uiviewcontroller,那么就是能够获取到这个controller的实例。那么就可以去看看有没有类似的属性可以拿到。相关文章推荐
- 如何在present出来的viewController里使用push方法
- 使用presentViewController跳转到上上个页面,实现多层跳转的两种方法。
- 如何在present出来的viewController里使用push方法
- [ios]使用self.view.window.rootViewController presentViewControllert跳转后的返回
- IOS——使用PresentViewController推送页面,各页面间跳转及遇到的问题
- IOS中集合视图UICollectionView中Decoration View的简易使用方法。
- iOS 表视图(UITableVIew)的使用方法(1)表视图的示例
- pushViewController/presentModalViewController/addSubView区别及使用方法
- IOS 表视图(UITableVIew)的使用方法(5)表视图的编辑功能(删除)
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- IOS 表视图(UITableVIew)的使用方法(2)名单的分段显示
- IOS: 正确使用PresentModalViewController
- IOS 表视图(UITableVIew)的使用方法(3)名单的索引显示
- SplitViewController只能做根视图,如何从一个ViewController跳转到SplitViewController
- IOS: 正确使用PresentModalViewController
- IOS UI:弹出视图/弹出模态 presentViewController与presentModalViewController(增加IOS 6.0说明)
- pushViewController/presentModalViewController/addSubView区别及使用方法
- IOS学习笔记(九)之UIAlertView(警告视图)和UIActionSheet(操作表视图)基本概念和使用方法
- IOS UI:弹出视图/弹出模态 presentViewController与presentModalViewController(增加IOS 6.0说明)