ios - 如何回到 Root View Controller

标签 ios swift cocoa-touch segue

我有四个 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/

相关文章:

ios - 缩小和适合 MapKit 中所有注释的最佳方法是什么

ios - UITableView 是否小于 HIG 的全宽?

ios - IOS 10上的Firebase无法从真实设备上的数据库加载数据

ios - 将自定义词典保存为用户默认值

ios - 使用 fetchrequest 谓词过滤掉某些核心数据项。

iphone - CATiledLayer 上的 CALayer,drawLayer :inContext never called

ios - 如何为依赖于使用 cocoapods 添加的其他框架的所有体系结构构建 cocoa touch 框架?

iphone - xcode - 使搜索栏出现/消失

ios - 使透明的stickyHeader像天气iOS一样

ios - 在 uiview 中显示引脚信息