如何调用另一个类中的协议方法
举例来说,我在一个类中声明了一个协议,并且我希望在不同的类中调用它。是否可以。
@protocol testProtocol
@required
- (void) showTestProtocol:(Call *)callObject;
@end
@interface TestClass1 : UITableViewController {
id<testProtocol> delegate;
}
@property (nonatomic, retain) id delegate;
在实现类中
@synthesize delegate;
实际上另一个类具有协议中声明的方法的定义。现在我如何在另一个类中使用这个协议来调用该方法?我需要所有其他类都应该使用此方法。
提前致谢
Say for example i have declared a protocol in one class and i want it to be called in different classes. Is it possible.
@protocol testProtocol
@required
- (void) showTestProtocol:(Call *)callObject;
@end
@interface TestClass1 : UITableViewController {
id<testProtocol> delegate;
}
@property (nonatomic, retain) id delegate;
in Implementation class
@synthesize delegate;
Actually another Class has the definition of the method declared in the protocol. Now how can i use this protocol in another class to call that method?. I need all the other classes should use this method.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想从另一个类(例如 ClassA)使用此协议中声明的方法,您首先要导入定义此协议的头文件 (.h)。然后,ClassA 必须遵守协议。如果您希望所有类都能够实现此方法,并且您不想显式确认此协议,那么您可能应该考虑使用使用类别的非正式协议。
If you want to use the methods declared in this protocol from another class (say ClassA), you to first import the header file (.h) in which this protocol is defined. And then, ClassA has to conform to the protocol. In case you want all classes to be able ti implement this method, and you don't want to explicitly confirm to this protocol, then you should probably be looking into using the informal protocol which use categories.