如何在Objective-C中声明事件和委托? [英] How to declare events and delegates in Objective-C?
问题描述
C#代码:
public delegate void ItemFound(ChunkDetails ObjChunkDetails);
public event ItemFound eventItemFound;
如何在Objective-C中声明上述事件委托机制?
How to declare the above event delegates mechanism in Objective-C?
我试图在Objective-c中声明委托方法,但仍然需要帮助.
I tried to declare delegates method in objective-c but still i need help.
Objective-C:
Objective-C:
+ (void) ItemFound:(ChunkDetails *)ObjItemDetails;
那么,如何在Objective-C的.h文件中声明事件和委托的签名?
So how to declare signature of events and delegates in .h file of Objective-C?
推荐答案
在示例A中,类A调用类B来执行操作,然后返回某些操作,我们将执行以下操作:
In the example where class A calls class B to perform an action and then return something we would do this:
B类的.h,它高于@interface
Class B's .h, this goes above the @interface
@protocol CLASSBNAMEDelegate <NSObject>
- (void) YOURMETHOD:(id) returnValue
@end
然后在@interface
下,我们添加一个委托属性:
Then under the @interface
we add a delegate property:
@property (nonatomic, weak) id < CLASSBNAMEDelegate > delegate;
在b.m类中,您要将消息发送回A类,您将:
In class b .m where you want to send a message back to Class A you would:
if ([self.delegate respondsToSelector:@selector(YOURMETHOD:)]) {
[self.delegate YOURMETHOD:value];
}
在使用B类的A类中,请确保像这样设置委托:
In Class A, where you use Class B be sure to set the delegate like so:
ClassB *b = [Class B etc....];
[b setDelegate:self];
在A类标题中,请确保您:
IN Class A Header make sure you:
@interface CLASSA : NSObject <CLASSBNAMEDelegate>
然后您需要响应选择器:
Then you would need to respond to the selector:
- (void) YOURMETHOD:(id) value{}
希望这对您有帮助...
Hope this helps...
这篇关于如何在Objective-C中声明事件和委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!