如何在 UIViewControllers 之间使用委托?

Posted

技术标签:

【中文标题】如何在 UIViewControllers 之间使用委托?【英文标题】:How to use delegates between UIViewControllers? 【发布时间】:2018-12-04 14:14:29 【问题描述】:

我已经刷完了:

Passing Data between View Controllers Passing data between two ViewControllers (delegate) - Swift Reload collection view data from another view class

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

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中追加到列表吗?

我还使用通知中心作为代表的替代方案,但没有成功。

【问题讨论】:

【参考方案1】:

您所做的几乎所有事情都是正确的,但是您将 ViewController2 的代表设置错了。

您实际上是在viewDidLoad 中设置ViewController2 的新实例的委托,但在moveToOtherVC 中,您将不同的ViewController2 呈现为UINavigationController 的根

改为设置ViewController2 的新实例的委托,并将此实例用作呈现navigationControllerrootViewController

@objc func moveToOtherVC() 
    let vc2 = ViewController2()
    vc2.delegate = self
    let navigationController = UINavigationController(rootViewController: vc2)
    present(navigationController, animated: true, completion: nil)

或者您可以通过将otherView 传递为rootViewControllernavigationController 来解决您的问题

let navigationController = UINavigationController(rootViewController: otherView)

【讨论】:

【参考方案2】:

像这样使用 ViewController1 函数-

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

【讨论】:

他没有使用故事板

以上是关于如何在 UIViewControllers 之间使用委托?的主要内容,如果未能解决你的问题,请参考以下文章

如何在IOS7中实现UIViewControllers之间的前后转换导航?

如何从“菜单”视图控制器在现有 UIViewControllers 之间导航?

如何在布局类似于fork的UIViewControllers之间导航和传递数据?

在 UIViewControllers 和 UISplitViewController 之间导航 [关闭]

在 UISplitViewController 和 UIViewControllers 之间导航 [重复]

在任何 UIViewControllers 之间交替?