gpt4 book ai didi

swift - 快速将数据传递给变量

转载 作者:行者123 更新时间:2023-11-28 13:22:48 24 4
gpt4 key购买 nike

我想将按钮标签分配给变量。然后我尝试将此数据发送到 SecondViewController 并将其分配给 var。

@IBAction func goToProducts(_ sender: UIButton) {

performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
categoryNumber = sender.tag }

将数据传递给 ProductsViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = categoryNumber
}
}

在 ProductsViewController 中,我创建了 var,但没有此数据。我打印这个变量并得到这个:

categoryNumber: 373

categoryNumb: Optional(24)

你能告诉我我该怎么做吗?

最佳答案

需要在performSegue之前先赋值

categoryNumber = sender.tag  
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)

或使用sender

performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)  

将数据传递给 ProductsViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = sender as! Int
}
}

关于swift - 快速将数据传递给变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58864604/

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