首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

IOS4 note 14 (三) Notifications

2012-07-20 
IOS4 note 14 (3) NotificationsNotificationsNotifications can be a way to communicate between object

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.

热点排行