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

标签 ios swift

我有一个带有 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/

相关文章:

iPad 启动图像在 sim 中有效,但在设备上无效

ios - 如何在 Swift 中正确使用类扩展?

ios - UIbutton iOS 的圆角 - Swift

ios - 如何在 iOS 11 或更高版本的 iPhone 中使用 ITMS 服务协议(protocol)?

ios - 在 api 类之外显示与 alamofire 相关的警报

ios - iAd 横幅无法正确加载

ios - 在 Swift 中使用 Facebook 异步显示工具包

xcode - 有没有办法简化键入 Swift 尾随闭包?

swift - 从 Firebase 中提取特定用户

ios - WebThread JavaScriptCore 崩溃——这个崩溃报告意味着什么