performSegue将下一个视图控制器推送到不需要的视图层次结构

时间:2017-12-26 10:11:03

标签: ios swift uiviewcontroller uinavigationcontroller uistoryboard

我正在构建一个基于导航的应用程序,您可以使用底部选项卡进行导航,每个选项卡都有自己的导航堆栈。 大多数部分按预期工作,但一个特定的选项卡表现出意外。

发生的事情是:

在浏览了几个视图控制器之后,让我们说[A] [B] [C]和[D],下一个控制器[E]突然从导航堆栈中消失。 当它发生时,视图看起来像是模态呈现的,导航堆栈看起来像这样。

(lldb) po [[[UIWindow keyWindow] rootViewController] _printHierarchy]

<MyApp.CRTabBarController 0x112026800>, state: disappeared, view: <UILayoutContainerView 0x111e1fa00> not in the window
   | <UINavigationController 0x112027a00>, state: disappeared, view: <UILayoutContainerView 0x111e23f50> not in the window
   |    | <MyApp.X1_ViewController 0x111d12490>, state: disappeared, view: <UIView 0x111e32d70> not in the window
   |    |    | <MyApp.InsideX1_ViewController 0x112096200>, state: disappeared, view: <UIView 0x111e35d90> not in the window
   | <UINavigationController 0x11200aa00>, state: disappeared, view: (view not loaded)
   |    | <MyApp.X2_ViewController 0x111d22360>, state: disappeared, view: (view not loaded)
   | <UINavigationController 0x11205a200>, state: disappeared, view: (view not loaded)
   |    | <MyApp.X3_Controller 0x11205a800>, state: disappeared, view: (view not loaded)
   | <UINavigationController 0x112059600>, state: disappeared, view: (view not loaded)
   |    | <MyApp.X4_ViewController 0x111e0cea0>, state: disappeared, view: (view not loaded)
   | <UINavigationController 0x112058e00>, state: disappeared, view: <UILayoutContainerView 0x111d50370> not in the window
   |    | <MyApp.A_ViewController 0x111d25f70>, state: disappeared, view: <UIView 0x116e0ad90> not in the window
   |    |    | <MyApp.PageViewController 0x112891400>, state: disappeared, view: <_UIPageViewControllerContentView 0x113feb0a0> not in the window
   |    |    |    | <MyApp.InnerPageViewController 0x111ee3bf0>, state: disappeared, view: <UIView 0x111e6e000> not in the window
   |    | <MyApp.B_ViewController 0x113ee8280>, state: disappeared, view: <UIView 0x116e37cf0> not in the window
   |    | <MyApp.C_Controller 0x118f06f10>, state: disappeared, view: <UIView 0x113ee46f0> not in the window
   |    | <MyApp.D_ViewController 0x116e5f580>, state: disappeared, view: <UIView 0x118f2fe70> not in the window
   + <E_ViewController 0x116ecff30>, state: appeared, view: <UIView 0x111ee43f0>, presented with: <_UIFullscreenPresentationController 0x116e13420>

顶部的四个导航控制器是非活动标签,这里不感兴趣。在最后一个标签的导航中,您可以看到A_ to D_ - ViewController's。然后,E_ViewController位于不同的导航层次结构中,这就是问题所在。

代码很简单,D_ViewController只是像这样调用performSegue

self.performSegue(withIdentifier: "goto_E", sender: self)

我认为Segue设置也很正常。它只是指定id和“Show”作为样式。 Segue已从D_ViewController连接到E_ViewController,因此可以将其称为performSegue's标识符arg。

enter image description here

有没有人有这种经历?有关可能原因的猜测吗?

2 个答案:

答案 0 :(得分:1)

//删除segue并实例化

extension UIViewController {

     class func instantiate(fromStoryboard name: String, id: String) -> Self? {
         return instantiateHelper(fromStoryboard: name, id: id)
     }

     private class func instantiateHelper<T>(fromStoryboard name: String, id: String) -> T? {
         let storyboard = UIStoryboard(name: name, bundle: nil)
         let controller = storyboard.instantiateViewController(withIdentifier: id) as? T
         return controller
     }
}

使用: -

 guard let vc = ViewController.instantiate(fromStoryboard: "Main", id: "ViewController") else { return }
 self.navigationController?.pushViewController(vc, animated: true)

答案 1 :(得分:0)

感谢您的评论,但我自己已经解决了。 说这很尴尬,但原因很愚蠢,就是在调用popViewController之前调用performSegue

由于我的视图结构发生了变化,这个流行音调就在那里,因为写在分开的地方很难注意到。

虽然这不是一个直接的答案,但直接从Storyboard实例化的方法有助于澄清问题。谢谢,Pratyush。

但是在将segue结果拉到这种行为之前弹出视图控制器是完全出乎意料的。 希望这可以暗示其他人的问题。