iOS:什么是 Objective-C 中的事件侦听器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10492138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS: what is the equivalent of an event listener in Objective-C?
提问by Jhorra
In some of my learning I saw someone mention that in your view controller you can have a model and have some sort of listener on the model for changes in it. I don't think I'm using the right names for these, which is probably why my searches haven't turned up anything. Essentially I want to move my server calls from the controllers into my models, but I need some sort of listener on them to know when the call is complete to update my views.
在我的一些学习中,我看到有人提到在你的视图控制器中你可以有一个模型,并在模型上有某种监听器来改变它。我认为我没有为这些使用正确的名称,这可能就是为什么我的搜索没有出现任何结果的原因。本质上,我想将我的服务器调用从控制器移动到我的模型中,但是我需要某种监听器来了解调用何时完成以更新我的视图。
回答by manuelBetancurt
look into delegates delegates tutorial
看看代表 代表教程
or blocks a bit more advanced basic blocks tutorial
或块更高级的 基本块教程
just start with delegates,
就从代表开始吧
you can also use NSNotification NSNotification tutorialbut is not recommended as it broadcast to every class, and you might only need to send messages to a specific class not every one
您也可以使用 NSNotification NSNotification 教程但不推荐,因为它会广播到每个班级,并且您可能只需要向特定班级发送消息而不是每个班级
回答by csblo
Belong to C# world, i have to go to objective c (for my job). I think the event equivalent in objective c is this implementation :
属于 C# 世界,我必须去目标 c(为了我的工作)。我认为在目标 c 中等效的事件是这个实现:
Create protocol with all your event's methods :
使用所有事件的方法创建协议:
@protocol MyDelegate <NSObject>
- (void)myEvent;
@end
In your class which should send the event, add :
在应该发送事件的类中,添加:
@interface MyClassWichSendEvent : NSObject
@property (nonatomic, retain) IBOutlet id<MyDelegate> delegate;
@end
Raising the event where you want, for example :
在您想要的地方引发事件,例如:
- (IBAction)testEvent:(NSButton*)sender
{
[self.delegate myEvent];
}
Now in your listener class, you should listen the events like this :
现在在你的监听器类中,你应该监听这样的事件:
Add the protocol to your class that listening
将协议添加到您的类中
@interface Document : NSDocument<MyDelegate>
In the implementation, on init or in interface builder, you must link delegate of your object instance to listen with self of your class which listen.
在实现中,在 init 或接口构建器中,您必须将对象实例的委托链接到侦听类的 self 。
In code
在代码中
-(void)awakeFromNib
{
myObjToListen.delegate = self;
}
- In Interface Builder -> IBOutlet from delegate to your listen's class.
- 在 Interface Builder -> IBOutlet 从委托到您的监听类。
And finally, implement your method in your listener class :
最后,在你的监听器类中实现你的方法:
- (void)myEvent
{
NSLog(@"i have listen this event !");
}
Sorry for my english, i hope that help people who went from java or C#.
对不起,我的英语,我希望能帮助那些从 Java 或 C# 去的人。
回答by spring
You're looking for KVO - key/value observing:
您正在寻找 KVO - 键/值观察:
http://nachbaur.com/2011/07/29/back-to-basics-using-kvo/
http://nachbaur.com/2011/07/29/back-to-basics-using-kvo/
Delegates + Notifications are good for communicating between objects but they don't automatically send msgs when a value changes (which from your question, that is what you are asking about)
委托 + 通知适用于对象之间的通信,但它们不会在值更改时自动发送消息(从您的问题来看,这就是您要问的)
回答by rooster117
I think you may be looking for NSNotificationCenter which is a way to pass messages to whoever may be listening. So you can send out a notification from an event in your model and just listen for it in your controllers. A cleaner method might be to implement your own protocol with delegates.
我认为您可能正在寻找 NSNotificationCenter,这是一种将消息传递给可能正在收听的人的方式。因此,您可以从模型中的事件发出通知,然后在控制器中监听它。一种更简洁的方法可能是使用委托来实现您自己的协议。
回答by Joe
Objective C uses delegates
Objective C 使用委托
This post has a nice example: How do I create delegates in Objective-C?
这篇文章有一个很好的例子:如何在 Objective-C 中创建委托?