为什么NSNotification中的Observer调用了两次....? [英] Why the Observer in NSNotification called twice....?

查看:159
本文介绍了为什么NSNotification中的Observer调用了两次....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果类是一个自定义类,发布通知后,对应于观察者的选择器被调用两次..有更好的解决方案,所以选择器只被调用一次?

解决方案

如果观察者类通过名称注册了一个通知,而不是针对特定的对象,它将接收多个消息,因为每次通知



同样,如果观察者针对特定对象注册,但不是针对命名的通知,那么每次有



被通知遥远状态变化的另一种机制是Key Value观察 - http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html


If the class is a custom class, after posting the notification, the selector corresponding to the observer is called twice.. Is there any better solution so that the selector is called only once?

解决方案

If the observer class is registered for a notification by name, but not against a specific object, it will receive multiple messages, as it will be invoked every time the notification occurs, regardless of the originating object.

Likewise, if the observer is registered against a specific object, but not against a named notification, it will be messaged every time there is a notification concerning that object.

An alternative mechanism for being informed of changes in a distant state, is Key Value observing -http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

这篇关于为什么NSNotification中的Observer调用了两次....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆