gpt4 book ai didi

ios - 从 "Over Current Content"ViewController 返回时如何触发一些代码?

转载 作者:搜寻专家 更新时间:2023-11-01 06:01:39 24 4
gpt4 key购买 nike

我的应用中有两个ViewController,ViewController和ViewController2

在 ViewController 中,一个按钮设置 Present Modally segue to "ViewController2"

并且 ViewController 覆盖 viewWillAppear

override func viewWillAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print("will appear")
}

在 ViewController2 中,返回按钮

@IBAction func close(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}

现在它仍然可以触发 viewWillAppear 然后我从 ViewController2 返回到 ViewController

如果我将 ViewController2 的显示从 Full Screen 更改为 Over Current ContextviewWillAppear 将不会被触发

返回时如何触发一些代码?

最佳答案

你可以在不放弃 Storyboard转场的情况下做到这一点,但你仍然必须在 ViewCONtroller2 中设置 will/did Disappear 处理程序:

class ViewController: UIViewController {
...
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController2 {
(segue.destination as? ViewController2).onViewWillDisappear = {
//Your code
}
}
}
}

class ViewController2: UIViewController {
var onViewWillDisappear: (()->())?

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
onViewWillDisappear?()
}
...
}

关于ios - 从 "Over Current Content"ViewController 返回时如何触发一些代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48185775/

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