gpt4 book ai didi

iphone - 如何用两个相同的方法编写委托(delegate)协议(protocol)

转载 作者:行者123 更新时间:2023-11-28 18:41:19 24 4
gpt4 key购买 nike

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

@protocol AddContentViewControllerDelegate <NSObject>

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

最佳答案

每当你声明一个协议(protocol)时,你也必须为它创建一个委托(delegate)

id <AddContentViewControllerDelegate > delegateAddContent

并在.m文件中创建其属性ans synthesize

@property (nonatomic) id delegateAddContent

以.m 为单位

@synthesize delegateAddContent

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

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

可能有一些你想发送数据的类。那个类必须符合你的协议(protocol)例如-->

@interface ClassName : SuperClass<AddContentViewControllerDelegate >

然后你必须实现协议(protocol)的方法。/例如-->-

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

此外,符合协议(protocol)的类必须拥有协议(protocol)所有权

yourclassconformingprotocol.delegateController=self.

你也可以在protocol中通过@required定义required方法,通过@optional定义optional

参见 Apple's documentation on protocol

关于iphone - 如何用两个相同的方法编写委托(delegate)协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10563904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com