gpt4 book ai didi

ios - 传递滞后于一个 UIAction 按钮的值

转载 作者:行者123 更新时间:2023-11-28 15:59:04 25 4
gpt4 key购买 nike

我有一个带有 6 个按钮的 View Controller ,所有这些按钮都连接到另一个带有 TableView 的 View Controller 。根据按下的按钮,不同的值将传递给带有 TableView 的 View Controller :

@IBAction func englishButton(_ sender: Any) {
valueToPass = "English"
}


@IBAction func scienceButton(_ sender: Any) {
valueToPass = "Science"
}

@IBAction func historyButton(_ sender: Any) {
valueToPass = "History"
}


@IBAction func foriegnLanguageButton(_ sender: Any) {
valueToPass = "Foriegn Language"
}


@IBAction func mathButton(_ sender: Any) {
valueToPass = "Math"
}


@IBAction func otherSubjectsButton(_ sender: Any) {
valueToPass = "Other Subjects"
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?){

if (segue.identifier == "english") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
if (segue.identifier == "science") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
if (segue.identifier == "history") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
if (segue.identifier == "foriegnLanguage") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
if (segue.identifier == "math") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
if (segue.identifier == "otherSubjects") {
// initialize new view controller and cast it as your view controller
let viewController = segue.destination as! BuyMenuController
// your new view controller should have property that will store passed value
viewController.passedValue = valueToPass
}
}

这里的问题是,每当我按下 scienceButton 时,传递给下一个 View Controller 的值要么是 nil,要么是之前传递的值。再次单击时,传递的值就像其假设的那样是“科学”。这只发生在那个按钮上,知道为什么会这样吗? (我也尝试过使用 if-else if 结构来设置它)。

最佳答案

您不应该将 @IBAction 方法与直接链接到 Interface Builder 中的操作的 segue 结合使用,因为 segue 可能在函数执行之前执行。

您应该将 segue 链接到 View Controller 对象,并在设置变量后在 @IBAction 函数中以编程方式执行它。

关于ios - 传递滞后于一个 UIAction 按钮的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41336234/

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