gpt4 book ai didi

swift - UIViewController 生命周期

转载 作者:行者123 更新时间:2023-11-30 10:14:29 25 4
gpt4 key购买 nike

我刚刚使用 View Controller 委托(delegate)尝试了一些东西并了解了它们的功能。我想尝试一件事:

我编写了一个函数,通过使用 segues 以编程方式展开 viewController。这些 View 位于 navigationViewController 内。另外,我使用以下方法在目标 VC 中设置了一个实例变量:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "unwindToRootSegue" {
let destinationController = segue.destinationViewController as! ViewController
destinationController.callLaunched = true
}
}

然后我想检查该值是否已在目标 VC 中设置。并想到使用viewWillAppear函数。但这没有效果。当我回滚到的 View (segue 的目标 VC)已加载/显示以执行进一步操作时,应该使用什么方法来通知。

我的目标是,当实例变量设置为 true 时执行操作。所以我的想法是,在加载目标 View 后立即检查实例变量。

最佳答案

为了使用展开转场,您首先要在目标 View Controller 中设置一个方法。

@IBAction func unwind(segue: UIStoryboardSegue) {
// unwind
}

请注意,此方法放置在目标 View Controller 中。正在展开的 View Controller 。

从这里开始,我们不需要segue的destinationViewController,而是sourceViewController

@IBAction func unwind(segue: UIStoryboardSegue) {
if let source = segue.sourceViewController as MySourceViewController {
self.someProperty = source.someOtherProperty
}
}

但是,如果我们只想在此处放松后做某事,无论如何,我们可以完全忽略segue(看起来就像您实际上在那里做的事情,除了认为您需要引用destinationViewController):

@IBAction func unwind(segue: UIStoryboardSegue) {
self.callLaunched = true
}

关于swift - UIViewController 生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31003362/

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