Reputation: 679
I've succesfully managed to pass one piece of data (one String variable, one Int variable etc.) between view controllers with delegate functions. However, I haven't managed to pass various pieces of data through delegate functions.
I get the following error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x7faea770db60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key receivingAmountLabel.'
If I delete the receingAmountLabel
, the error goes to another UI element. If I remove that element, it continues to another.
All UI elements are connected as they should be. The relevant piece of code look like this:
FirstVC.swift class 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)
}
}
}
}
I get the same problem when trying to pass a dictionary in the delegate.
Upvotes: 0
Views: 72
Reputation: 96
make sure receivingAmountLabel outlet is connected to FirstVC
Upvotes: 0
Reputation: 401
Use below simplified code:
if let del = delegate, let stringData = stringTF.text, let amountData = amountTF.text {
del.userDidEnterData(stringData: stringData, amountData: amountData)
dismiss(animated: true, completion: nil)
}
Upvotes: 1