读书人

IOS4 note 14 (三) Notifications

发布时间: 2012-07-20 10:38:30 作者: rapoo

IOS4 note 14 (3) Notifications

Notifications

Notifications can be a way to communicate between objects that are?conceptually distant from one another without bothering to provide any way for one?to see the other. Using a notification in this way may seem lazy, an evasion of your?responsibility to architect your objects sensibly. But sometimes one object doesn’t need?to know, and indeed shouldn’t know, what object it is sending a message to.

[[NSNotificationCenter defaultCenter] addObserver:self

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ?selector:@selector(toggleEnglish:)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??name:TOGGLE_ENGLISH

? ? ? ?object:nil];

(TOGGLE_ENGLISH is #defined as @"toggleEnglish" in a header.) When the user taps the?button to toggle the visibility of the English translation, the button’s action causes the?ScrollViewController’s toggleEnglish: method to be called. That method contains this?line:

[[NSNotificationCenter defaultCenter] postNotificationName:TOGGLE_ENGLISH

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??object:self];

or

[[NSNotificationCenter defaultCenter] postNotificationName:TOGGLE_ENGLISH

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? ??object:self

?userInfo:nil];

?

Be sure to invoke?removeObserver:?or?removeObserver:name:object:?before?

notificationObserver?or any object specified?in?addObserver:selector:name:object:?is deallocated.

读书人网 >操作系统

热点推荐