ios - View Controller 关闭并重新出现以前的 View Controller

标签 ios swift uiviewcontroller

我是 iOS 开发和 Swift 的新手,我对 View Controller 事件有疑问,当顶部 View Controller 关闭并再次显示之前的 View Controller 时,应该自动调用什么事件?

实际上我的意思是我有 View Controller A 和 View Controller B,我单击 View Controller A 的一个按钮(假设转到 B View Controller )并移动到 View Controller B 然后执行 View Controller B 的一些任务并单击一个按钮(假设返回)在那个时候我关闭 View Controller B 然后应用程序自动显示 View Controller A,在这种情况下我必须触发一种方法,但我的问题是 View Controller A 它怎么知道它会重新出现以显示?

最佳答案

根据您的情况,

View Controller A 知道它通过实现 UIViewController 两个方法之一重新出现 - 取决于您的要求-:

Notifies the view controller that its view is about to be added to a view hierarchy.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // ...
}

或者

Notifies the view controller that its view was added to a view hierarchy.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // ...
}

请注意,这两个方法也会在 View Controller 被呈现/推送时执行,而不仅仅是在被解雇/弹出之后。

在您的情况下,您需要知道 View Controller B 是否正在消失或已经消失。为此,您还可以实现 UIViewController 方法之一 - 也 - 取决于您的要求 -:

Notifies the view controller that its view is about to be removed from a view hierarchy.

或者

Notifies the view controller that its view was removed from a view hierarchy.


因此,假设当前呈现的 View Controller 是 View Controller B 并且它将被关闭,那么组合两个 View Controller 之间的方法调用的层次结构应该如下:

View Controller B => viewWillDisappear(_:)

View Controller A => viewWillAppear(_:)

View Controller B => viewDidDisappear(_:)

View Controller A => viewDidAppear(_:)

关于ios - View Controller 关闭并重新出现以前的 View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47406684/

相关文章:

objective-c - 初始化变量并在 for 循环中更改其值后访问它

ios - 在另一个 UIViewController 中显示缩放的 UIViewController

ios - 响应按钮事件更新图像

iphone - 如何在 iPhone 上获取所有已注册的 URL 模式?

ios - 我将如何构建 ios8 自定义键盘来选择图像

ios - 获取 NSString 的前两个字符

swift - 当 ContentMode = AspectFill 时在 UIImageView 中获取 UImage 的大小

ios - 对象层次结构导致 Swift 内存泄漏

ios - Swift - 从选项卡栏 Controller 更新 View Controller 内的变量

ios - Xcode Debug View Hierarchy 查找 viewcontroller