简体   繁体   English

如果我有两个实现协议的视图控制器,如何指定我的第三个控制器的委托者?

[英]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.

相关问题 我正在尝试使用协议和委托模式,它将我的数组中的数据传递回父视图 controller - I'm trying to use a protocol and delegate pattern which will pass the data in my array back to the parent view controller 如何保持透明视图控制器加载到所有其他视图控制器之上? - How do I keep a transparent view controller loaded overtop of all my other view controllers? 我的游戏中应该有几个View Controller? - How many View Controllers should I have in my game? 如何使用委托来指示哪个视图控制器行为与初始视图控制器相同? - How do I use the delegate to dictate which view controller behaves as the initial view controller? 如何设置一个简单的委托来在两个视图控制器之间进行通信? - How do I set up a simple delegate to communicate between two view controllers? 如何为导航控制器设置默认标题视图? - How do I set the default title view for my navigation controllers? 我在哪个视图控制器委托方法中请求API? - In which view controller delegate method do i request the api.? 我在导航控制器层次结构中具有视图控制器,但只希望第一个视图控制器上的导航栏-如何执行此操作? - I have view controllers in a navigation controller hierarchy, but only want the navigation bar on the first view controller - how do I do this? 我希望我的视图控制器具有相互通信的自定义UIView的相同组合 - I want my view controllers to have the same combination of custom UIViews which communicate with each other Swift:我的代理协议方法需要在另一个视图控制器上执行 - Swift: My Delegate Protocol Methods Need to Execute on Another View Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM