gpt4 book ai didi

ios - 准备 Segue 函数无法正确传递数据

转载 作者:行者123 更新时间:2023-11-30 11:11:37 32 4
gpt4 key购买 nike

我的 prepareForSegue 方法没有将数据传递到目标 View Controller 。

var buttonsDictionary = [Int: UIButton]()

func createButtonArray() {
for item in statTitles {
let statisticButton = StatButton()

statisticButton.layer.cornerRadius = 10
statisticButton.backgroundColor = UIColor.darkGray
statisticButton.setTitle(String(item.value), for: UIControlState.normal)
statisticButton.setTitleColor(UIColor.white, for: UIControlState.normal)
statisticButton.titleLabel?.font = UIFont.systemFont(ofSize: 43)
statisticButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0)
statisticButton.contentHorizontalAlignment = .left

statisticButton.addTarget(self, action: #selector(displayStatDetail), for: .touchUpInside)

statisticButton.buttonIndex = item.key

buttonsDictionary[item.key] = (statisticButton) // Assign value at item.key

print(statisticButton.buttonIndex)
}
}

func viewSavedStatistics() {
for button in buttonsDictionary {
statisticsView.addArrangedSubview(button.value)
}
}

@objc func displayStatDetail() {
self.performSegue(withIdentifier: "StatDetailSegue", sender: UIButton())
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "StatDetailSegue" {
if let destinationVC = segue.destination as? StatDetailViewController,
let index = (sender as? StatButton)?.buttonIndex {
destinationVC.statID = index
print("Destination STATID: \(destinationVC.statID)")
}
}
}

以上代码全部写在ViewController类中。StatButton 是一个自定义 UIButton 类。准备意味着传递点击按钮的 buttonIndex ,但只传递 0 并且不 print 所以我不认为正在调用它。

最佳答案

您的发件人是 UIButton实例,它没有您需要的任何信息。相反,传递调用选择器的按钮。

@objc func displayStatDetail(_ sender: StatisticButton) {
self.performSegue(withIdentifier: "StatDetailSegue", sender: sender)
}

您需要在循环中像这样更改目标选择器。

statisticButton.addTarget(self, action: #selector(displayStatDetail(_:)), for: .touchUpInside)

关于ios - 准备 Segue 函数无法正确传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134795/

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