我有四个 View Controller A、B、C 和 D
我从A到B到C再到D
let newViewContoller: B = self.storyboard?.instantiateViewControllerWithIdentifier("B") as! B
self.presentViewController(newViewContoller, animated: true, completion:nil)
从D回到A
我正在使用以下内容
self.view.window!.rootViewController?.dismissViewControllerAnimated(true, completion: nil)
我面临的问题是,
正在调用 viewController B 和 viewController C 的 viewDidAppear
。
我不知道为什么会这样?返回初始 UIViewController
的正确方法是什么。
最佳答案
您的 ViewController A、B、C 和 D 位于堆栈数据结构中,该结构实现了后进先出。这意味着要通过关闭 ViewController 到达 ViewController A,它将按此顺序弹出 ViewController D、C、B。在这种情况下,从堆栈中弹出的 ViewController C 和 B 将在一瞬间成为最顶层的 ViewController,这将触发 viewWillAppear() 和 viewWillDissapear() 函数。苹果文档:
calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack.
相反,如果你有一个导航 Controller 并且不想让 C 和 B 出现,你应该做的是从当前显示的 viewController (D) 中调用 popToRootViewController(),它将弹出当前的,然后释放 C 和 B 和然后显示 A。
关于ios - 如何回到 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819760/