在容器中的同级视图控制器之间使用委托 [英] Using delegate between sibling view controllers in containers

查看:92
本文介绍了在容器中的同级视图控制器之间使用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个使用三个容器显示不同内容的应用,但是在容器之间进行通讯时遇到了麻烦。我成功地使用segue将一个容器中的按钮上的一些信息发送到另一个容器,但是该信息的一部分也必须中继到第三个容器。为此,我想使用一个委托,但无法将正确的ViewController引用到委托变量。

I'm trying to make an app that uses three containers to show different content, but I'm having trouble communicating between the containers. I succeeded to use a segue to send some information at the tap of a button in one container to another container, but part of this information also has to be relayed to the third container. For this I wanted to use a delegate, but I cannot reference the right ViewController to the delegate variable.

所以我想要的内容如下:

So what I want goes as follows:


  1. 轻击CollectionViewCell,触发对TableVC的选择

  2. TableVC接收信息并更新表

  3. TableVC触发第三个VC中的委托函数

  4. 第三个VC获取一些信息并更新视图

  1. CollectionViewCell tapped, triggering segue to TableVC
  2. TableVC receives information and updates the table
  3. TableVC triggers delegate function in third VC
  4. Third VC takes in some info and updates view

在上面,我设法获得了1和2的值,但是卡在了3。

In the above I have managed to get 1 and 2 to work, but got stuck at 3.

我的协议如下:

protocol PurchaseDelegate {
    func addToTotalAmount(product : Product)
}

在TableVC中,我声明了 var委托:PurchaseDelegate? = nil ,并在由segue触发的IBAction中: delegate?.addToTotalAmount(product)

In the TableVC I have declared var delegate : PurchaseDelegate? = nil and in the IBAction triggered from the segue: delegate?.addToTotalAmount(product)

在第三个VC中,我实现了以下委托:

In the third VC I have implemented the delegate as follows:

class thirdVC:UIViewController,PurchaseDelegate {
    func addToTotalAmount(product : Product) {
        println("Adding....")
    }
}

所有三个容器都位于主VC中,该主VC在应用程序中执行一些初始操作。

All three containers are within a main VC that does some initial stuff in the application.

我的问题是,我不知道

预先感谢。

推荐答案

在@Anna Dickinson的启发下,我做了进一步的搜索之后,最终找到了解决问题的方法。

I ended up finding the solution to the problem after a bit further searching with inspiration from @Anna Dickinson.

首先,必须在情节提要中正确订购容器。其视图控制器实现委托协议的容器必须首先位于列表中,然后是另一个视图控制器。

Firstly, the containers must be ordered correctly in the storyboard. The container whose view controller implements the delegate protocol must be first in the list and then the other view controller further down.

然后,在主视图控制器中-视图控制器对于具有容器的视图-实现了prepareForSegue函数,因为它将在初始化容器时触发。

Then, in the main view controller - the view controller for the view with the containers - the prepareForSegue function is implemented, since it will be triggered as the containers are initialized.

所有这些代码均如上所述,但是主视图控制器将如下所示:

This all of the code remains as above, but the main view controller will be something like the following:

class MainViewController: UIViewController {
    var actionVC : FirstViewController! // This is the one, that implements the delegate protocol
    var tableVC : SecondViewController! // This is the one, that has a delegate variable
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if(segue.identifier == "firstVC"){
            self.actionVC = segue.destinationViewController as FirstViewController
        } else if(segue.identifier == "secondVC"){
            self.tableVC = segue.destinationViewController as SecondViewController
            self.tableVC.delegate = self.actionVC
        }
    }
}

我不知道这是正确的方法还是最佳方法,但是它完全可以满足我的需求。

I'm not sure if the is the right, nor the best way to do this, but it works perfectly for what I need.

这篇关于在容器中的同级视图控制器之间使用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆