[英]If I have two view controllers that implements a protocol, how do I specify which my is the delegate for my third controller?
I have a protocol to exchange data after it is edited. 编辑后,我有一个协议可以交换数据。 This protocol is implemented in two different view controllers.
该协议在两个不同的视图控制器中实现。
The protocol has a single function called taskEdited that sets a value. 该协议具有一个称为taskEdited的函数,用于设置值。
In a third view controller that is is pushed from the first, I have a delegate property in my header file: 在从第一个推入的第三个视图控制器中,我的头文件中有一个委托属性:
@property (weak, nonatomic) id <taskEditProtocol> delegate;
And in the .m file I call the method as follows... 在.m文件中,我按如下方式调用该方法...
[self.delegate taskEdited:self.taskForEdit];
The result is that the method gets called in the first view controller which in this case is what I want. 结果是该方法在第一个视图控制器中被调用,在这种情况下,这就是我想要的。 But how do I specify which controller is the delegate of the third view controller?
但是,如何指定第三个视图控制器的代表是哪个控制器呢?
You must set the delegate property. 您必须设置委托属性。 Most likely in a
prepareForSegue:sender:
method. 最有可能在
prepareForSegue:sender:
方法中。
The view controller that you want to be the delegate needs to have a reference to the third view controller (which it can obtain in prepareForSegue:sender:
. And then you simply set the property as you would set any other property on any other object. 要成为委托的视图控制器需要引用第三个视图控制器(可以在
prepareForSegue:sender:
获得该引用),然后像设置其他对象上的任何其他属性一样,简单地设置该属性。
You need to manually set it in the moment you create the instance of the third controller. 创建第三个控制器的实例时,需要手动设置它。 Assuming you instantiate the third controller from the controller you want to be the delegate, it would look like this.-
假设您要从要成为其代表的控制器实例化第三个控制器,则看起来像这样。
thirdController.delegate = self;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.