gpt4 book ai didi

ios - 为什么我的 prepareForSegue 方法没有被调用?

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

当我按下这个导航 Controller 中的后退按钮时,我想将开关的状态发送到不同 View Controller 中的变量。但是,未调用 prepareForSegue 方法。我该如何解决这个问题?

enter image description here

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("here")
let secondViewController:ViewController2 = segue.destination as! ViewController2

if undoSwitch.isOn {
secondViewController.undoControls[0] = true
}
else {
secondViewController.undoControls[0] = false
}
}

最佳答案

后退按钮不同于segue。如果你想在从当前 View Controller 返回时重新加载 undoSwitch。其中一种方法是使用静态全局变量。有点像

class AppVar {
static var controlState = true
}

在你需要的地方更新你的 AppVar.controlState 并添加它

if AppVar.controlState {
secondViewController.undoControls[0] = true
}
else {
secondViewController.undoControls[0] = false
}

ViewController2viewWillAppear 因为 ViewController2 被保留并存储在堆栈中。当您导航回它时,将调用 viewWillAppear

关于ios - 为什么我的 prepareForSegue 方法没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44895728/

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