普通的通知使用
注冊觀察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificationAction) name:@"ThisIsANoticafication" object:nil];
發送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"ThisIsANoticafication" object:nil];
傳遞帶參數的通知
在發送通知時設置object參數
[[NSNotificationCenter defaultCenter] postNotificationName:@"ThisIsANoticafication" object:@{@"parameter1":@"1",@"parameter2":@"2"}];
這里傳入了一個字典,那么如何在接收通知的時候得到這個字典呢
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificationAction:) name:@"ThisIsANoticafication" object:nil];
- (void)getNotificationAction:(NSNotification *)notification{ NSDictionary * infoDic = [notification object]; // 這樣就得到了我們在發送通知時候傳入的字典了 }
當然傳入的參數可以是其他類型。