gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:08:37 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com