如何快速将委托值添加到表视图

时间:2019-05-08 17:02:58

标签: swift delegates tableview

在这里,我向firstVC tableview发送secondVC文本字段值,但在tableview中没有收到委托值。

这是我的代码:

在firstVC中:

import UIKit

class CreateBusinessViewController: UIViewController, MyDataSendingDelegateProtocol, UITableViewDelegate, UITableViewDataSource {


    @IBOutlet weak var tableView: UITableView!

    var iteamsArray = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    func sendDataToFirstViewController(myData: String) {
        self.iteamsArray.append(myData)
        print(iteamsArray)
    }

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


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return iteamsArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! BusinessTableViewCell
        cell.nameLabel.text = iteamsArray[indexPath.row]

        return cell
    }
}

在secondVC中:

protocol MyDataSendingDelegateProtocol {
    func sendDataToFirstViewController(myData: String)
}

class CreatePopUpViewController: UIViewController {
    var delegate: MyDataSendingDelegateProtocol?

    @IBOutlet weak var addTf: UITextField!

    @IBAction func saveButn(_ sender: Any) {
        self.delegate?.sendDataToFirstViewController(myData: addTf.text!)

        dismiss(animated: true, completion: nil)
    }
}

如何将secondVC文本字段数据添加到firstVC数组?

0 个答案:

没有答案