如何将值传递给@protocol
我正在尝试实施一个协议。
我查看了文档 这里,我理解这些概念,尽管我认为我遗漏了一些东西。
我正在尝试创建一个视图,用户点击表视图中的文件名,触发“didSelectRowAtIndexPath”,这将通知委托用户已选择一个文件(在委托中触发 didSelectFileName)并传递文件名。我已将协议声明如下;
@protocol FileList <NSObject>
- (void)didSelectFileName:(NSString *)fileName;
@end
我的问题是:
- 如何设置“fileName”值,以便在调用“didSelectFileName”时它具有当前值
- 如何告诉我的代码在委托中触发“didSelectFileName”。
I'm trying to implement a protocol.
I've looked at the documentation here and I understand the concepts although I think I'm missing a few things.
I'm trying to make a view that the user taps on a filename in a table view triggering 'didSelectRowAtIndexPath' which will in turn notify the delegate that the user has selected a file (triggering didSelectFileName in the delegate) and passing the fileName. I've declared the protocol as follows;
@protocol FileList <NSObject>
- (void)didSelectFileName:(NSString *)fileName;
@end
My questions are:
- How do i set the 'fileName' value so that when 'didSelectFileName' is called it has the current value in it
- How do I tell my code to trigger 'didSelectFileName' in the delegate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能只向协议发送消息(也不能设置值)。您将消息发送到符合该协议的类。
当您说一个类符合协议(
@interface MyClass : NSObject{ etc
)时,您可以使用符合协议中方法的选择器安全地将任何消息发送到该类。因此,如果我们以您的协议为例,我们可以有一个可以向委托发送消息的类:
只需确保您在委托中实现协议中的方法即可。
您不需要重新定义委托类接口中的方法。
以下是一些有关协议的好读物:
You cannot just send a message to a protocol (nor setting values). You send the message to a class that conforms to the protocol.
When you say a class conforms to a protocol (
@interface MyClass : NSObject <MyProtocol> { etc
) you can safely send any messages to the class with the selectors that conform to the methods in the protocol.So if we take your protocol for example we can have a class that can send messages to a delegate:
Just make sure you implement the methods that are in your protocol in you delegate.
You don't need to redefine the methods in the interface of your delegate class.
Here are some good reads about protocols:
//在表View方法中
//In table View method