NSNotificationCenter 在 init里面注冊這個通知,
NSString* const str = @"FuckMe";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(on:) name:str object:nil];
在dealloc里面移除這個通知的注冊:
[[NSNotificationCenter defaultCenter] removeObserver:self name:str object:nil];
以上為不帶參數的通知
一般在使用NSNotificationCenter的時候不使用參數,但是有些時候需要使用參數。
傳遞參數
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:searchFriendArray];
接收參數並獲取傳遞的參數
postNotificationName:通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。
object:傳遞的參數
- (void) test:(NSNotification*) notification
{
searchFriendArrary = [notification object];//通過這個獲取到傳遞的對象
}