简体   繁体   English

iOS中目标操作和键值观察器之间的区别

[英]difference between Target Action and Key Value Observer in iOS

What is the difference between the two? 两者有什么区别? To me both look like doing the same thing ie notify an event observer about some event. 对我来说,两者看起来都像做同一件事,即通知事件观察者某个事件。

I read this post http://www.raywenderlich.com/46988/ios-design-patterns regarding the actual pattern in use for the two above mentioned event handling techniques but failed to understand how command pattern is different from observer pattern or am I missing something. 我阅读了有关http://www.raywenderlich.com/46988/ios-design-patterns的文章,内容涉及上述两种事件处理技术所使用的实际模式,但无法理解命令模式与观察者模式有何不同?遗漏了什么。

My understanding of them is the next: In the case of target action, you have the Notification Center through which you pass your info between two classes. 接下来是我对它们的理解:对于目标操作,您具有通知中心,通过该中心您可以在两个类之间传递信息。 You have one class which sends out the notification to the Notification Center which will broadcast the info and the second class which has the observers will listen for any broadcast that matches its criteria. 您有一类将通知发送到通知中心的类,该中心将广播信息,而另一类具有观察者的类将收听符合其条件的所有广播。

While in the case of KVO you have an object which is capable of observing the change of one of it's properties. 在使用KVO的情况下,您拥有一个能够观察其属性之一变化的对象。 So no one is posting any notification in the case of KVO. 因此,对于KVO,没有人发布任何通知。 Hope this cleared things up at least a bit. 希望这至少可以使事情顺利进行。

In target-action, the object controlling the event is told what to trigger from that event. 在目标动作中,控制事件的对象被告知从该事件触发什么。 The most common use of this for iOS developers is not NSInvocation, but Interface Builder. 对于iOS开发人员,此功能最常见的用途不是NSInvocation,而是Interface Builder。 The sender is usually a UIControl. 发送者通常是UIControl。

In NSNotification, a notification is broadcast to whoever is listening for a particular event. 在NSNotification中,通知将广播给正在侦听特定事件的任何人。 These notifications are pretty generic, and can contain any information. 这些通知非常通用,可以包含任何信息。 The listeners may or may not care who the sender is. 收听者可能不在乎发件人是谁。

In KVO, the listeners pay attention to specific properties of a particular object. 在KVO中,侦听器注意特定对象的特定属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM