通行证与代表

问题描述:

我已经成功地管理视图控制器之间的多个值,以通过视图控制器之间的数据的一块(一个字符串变量,一个int变量等)代表函数。但是,我没有设法通过委托功能传递各种数据。通行证与代表

我得到以下错误:

*** 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.swift 类FirstVC: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) 
       } 
      } 
     } 
    } 

我得到了同样的问题当试图在委托中传递字典时。

+0

您没有设置正确的类的'UIViewController'在Interface Builder的。它是一个'UIViewController',而不是'FirstVC'。如果这是由于misworking连接(更名等),它应该有'[的setValue:forUndefinedKey:]:''没有[的setValue:forUndefinedKey:]:' – Larme

+0

您能通过再试一次再次移除并连接IBOutlets? –

+0

这个错误通常当厦门国际银行或故事板的IBOutlets是无效的情况(如果他们设立了然后重命名或删除等)。如前所述,请检查您的IB文件是否有无效的参考。 –

使用下面简化代码:

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

不是解决问题的办法,但感谢的忠告。 – Max

确保receivingAmountLabel出口连接到FirstVC