如何用两种相同的方法编写委托协议 [英] How to write a delegate protocol withe two same method

查看:206
本文介绍了如何用两种相同的方法编写委托协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些按钮被按下时将一些数据添加到两个不同的实体中,我正在使用委托,但是我不知道该怎么做。

I want to add some data into 2 different entities when certain button is pressed, I am using delegate,but I don't know how to do that.

@protocol AddContentViewControllerDelegate <NSObject>

- (void)AddContentViewControllerDidCancel:(AddContentViewController *)controller;
- (void)AddContentViewController:(AddContentViewController *)controller didAddPlayer:(FailedBankInfo *)info;
- (void)AddContentViewController:(AddContentViewController *)controller didAddPlayer:(FailedBankDetails *)details;
@end


推荐答案

每当您声明协议,您还要为相同的

Whenever you declare a protocol , you have also to create a delegate for the same

id <AddContentViewControllerDelegate > delegateAddContent

并在.m文件中创建其属性并合成

and create its property ans synthesize in the .m file

@property (nonatomic) id delegateAddContent

in .m

@synthesize delegateAddContent

现在,您必须通过协议方法发送您已经通过.m文件方法定义的数据。

now you will have to send the data through protocol method that you have already defined through your .m file methods.

[self delegateAddContent]AddContentViewControllerDidCancel:(AddContentViewController *)controller];

可能有一些类要发送数据。该类必须符合您的协议
eg - >

there might be some class where you want to send the data.That class must conform to your protocol e.g-->

@interface ClassName : SuperClass<AddContentViewControllerDelegate >

然后你将不得不实现protocol./
的方法eg - > -

and then you will have to implement the methods of the protocol./ eg -->-

 (void)AddContentViewControllerDidCancel:(AddContentViewController *)controller
{
//the data will be received in the parameters of the method of the protocol implemented.here in controller
}

的控制器方法的参数中收到。此外,符合协议的类必须具有协议所有权

Also the class that conforms to protocol must take the protocol ownership

yourclassconformingprotocol.delegateController = self。

yourclassconformingprotocol.delegateController=self.

您可以还可以通过@required定义协议中所需的方法,由@optional定义

You can also define the required methods in protocol by @required and optional by @optional

请参阅 Apple的协议文档

这篇关于如何用两种相同的方法编写委托协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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