如何在UIViewControllers之间使用委托?

时间:2018-12-04 14:14:29

标签: swift uiviewcontroller delegates

我已经搜寻:

但是找不到答案。基本上,我有两个视图控制器,正在尝试使用委托来更新数组并最终重新加载collectionview。

ViewController1

    class ViewController1: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, AppendDelegate {

    let cellID = "cell"

    var list = ["item1", "item2", "item3"]
    let otherView = ViewController2()

    override func viewDidLoad() {
        super.viewDidLoad()

        otherView.delegate = self
        print(list)

        let collection = UICollectionView(frame: view.bounds)
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellID)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.cyan

        view.backgroundColor = .white
        view.addSubview(collection)

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(moveToOtherVC))
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as UICollectionViewCell
        cell.backgroundColor = UIColor.black
        return cell
    }

    func appendItem(string: String) {
        list.append(string)
        print(list)
    }

    @objc func moveToOtherVC() {
        let VC2 = UINavigationController(rootViewController: ViewController2())
        present(VC2, animated: true, completion: nil)
    }
}

我的第二个视图控制器

    protocol AppendDelegate {
    func appendItem(string: String)
}

class ViewController2: UIViewController {

    var delegate: AppendDelegate?

    let textField: UITextField = {
        let tf = UITextField(frame: CGRect(x: 0, y: 0, width: 39, height: 20))
        tf.backgroundColor = .blue
        return tf
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(textField)

        self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(sendDataToCollection))

        textField.center.x = view.center.x
        textField.center.y = view.center.y
    }

    @objc func sendDataToCollection() {
        guard let text = textField.text else {return}
        self.delegate?.appendItem(string: text)
        textField.text = ""
        moveToVC()
    }

    @objc func moveToVC() {
        dismiss(animated: true, completion: nil)
    }

}

在第二个类中,self.delegate.appendItem的结果不应该在ViewController1中追加到列表中吗?

我还使用了通知中心代替代表,但没有运气。

2 个答案:

答案 0 :(得分:0)

您所做的几乎所有事情都正确,但是您将ViewController2的委托设置错误。

您实际上是在ViewController2中设置viewDidLoad的新实例的委托,但是在moveToOtherVC中您要提供不同的ViewController2作为UINavigationController的根< / p>

代替设置ViewController2的新实例的委托,并将此实例用作呈现rootViewController的{​​{1}}

navigationController

或者您可以通过将@objc func moveToOtherVC() { let vc2 = ViewController2() vc2.delegate = self let navigationController = UINavigationController(rootViewController: vc2) present(navigationController, animated: true, completion: nil) } 的{​​{1}}作为otherView的{​​{1}}来解决问题

rootViewController

答案 1 :(得分:0)

像这样使用ViewController1函数-

  @objc func moveToOtherVC() {
     let otherView = 
     self.storyboard?.instantiateViewController(withIdentifier: 
     "ViewController2") as! ViewController2
     otherView.delegate = self                                
     self.present(next, animated: true, completion: nil)
   }