在具有委托的视图控制器之间传递多个值

时间:2017-10-11 08:48:45

标签: ios swift xcode delegates

我已经成功地在视图控制器和委托函数之间传递了一个数据(一个String变量,一个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 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)
                }
            }
        }
    }

尝试在委托中传递字典时遇到同样的问题。

2 个答案:

答案 0 :(得分:1)

使用以下简化代码:

 private void button4_Click(object sender, EventArgs e)
    {
        addcon a = new addcon();
        sendMsg sm = new sendMsg();

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            isCellChecked = Convert.ToBoolean(dataGridView1.Rows[i].Cells[0].Value);
            if (isCellChecked == true)
            {
                sm.txtDstNo.Text = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value) + ";"; 
                MessageBox.Show("Successfully added.", "Status", MessageBoxButtons.OK, MessageBoxIcon.Information);
                sm.Show();

            }
        }


    }

答案 1 :(得分:0)

确保receivingAmountLabel插座连接到FirstVC