gpt4 book ai didi

ios - 在具有委托(delegate)的 View Controller 之间传递多个值

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

我已经成功地在具有委托(delegate)函数的 View Controller 之间传递了一段数据(一个 String 变量、一个 Int 变量等)。但是,我还没有设法通过委托(delegate)函数传递各种数据。

我收到以下错误:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7faea770db60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key receivingAmountLabel.'

如果我删除 receingAmountLabel,错误将转到另一个 UI 元素。如果我删除该元素,它会继续到另一个。

所有 UI 元素都按应有的方式连接。相关代码如下所示:

FirstVC.swiftFirstVC 类:UIViewController、DataSentDelegateMax {

@IBOutlet weak var receivingStringLabel: UILabel!
@IBOutlet weak var receivingAmountLabel: UILabel!

func userDidEnterData(stringData: String, amountData: Int) {
receivingStringLabel.text = stringData
receivingAmountLabel.text = String(amountData)
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showSecondVC") {
let secondVC = segue.destination as! SecondVC
secondVC.delegate = self
}
}

SecondVC.swift

protocol DataSentDelegateMax {
func userDidEnterData(stringData: String, amountData: Int)
}

@IBOutlet weak var stringTF: UITextField!
@IBOutlet weak var amountTF: UITextField!

var delegate: DataSentDelegateMax? = nil

@IBAction func sendButtonAction(_ sender: Any) {
if delegate != nil {
if (stringTF.text != nil) {
if (Int(amountTF.text!) != nil) {
let stringData = stringTF.text
let amountData = Int(amountTF.text!)
delegate?.userDidEnterData(stringData: stringData!, amountData: amountData!)
dismiss(animated: true, completion: nil)
}
}
}
}

尝试在委托(delegate)中传递字典时,我遇到了同样的问题。

最佳答案

使用下面的简化代码:

   if let del = delegate, let stringData = stringTF.text, let amountData = amountTF.text  {
del.userDidEnterData(stringData: stringData, amountData: amountData)
dismiss(animated: true, completion: nil)
}

关于ios - 在具有委托(delegate)的 View Controller 之间传递多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46683814/

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