標籤:io ar os sp strong on log bs ad
1.發送中心,在要發出通知訊息的地方
[[NSNotificationCenter defaultCenter] postNotificationName:@"Firstnotification" object:urlstr2];
參數:
postNotificationName:通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。
object:傳遞的參數
2.添加觀察者,接受通知的地方
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recievenotification:) name:@"Firstnotification" object:nil];
參數介紹:
addObserver: 觀察者,即在什麼地方接收通知;
selector: 收到通知後調用何種方法;
name: 通知的名字,也是通知的唯一標示,編譯器就通過這個找到通知的。
發送通知:調用觀察者處的方法。
3.調用方法
- (void)recievenotification:(NSNotification *)sender{
currenturl = [NSURL URLWithString:[sender object]];//擷取到傳遞的對象
NSLog(@"%@",currenturl);
}
4.你的觀察者如果對一些事件沒興趣了,應該從 NotificationCenter 中移除掉:
- (void)dealloc
{
//移除觀察者
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Firstnotification" object:nil];
}
附:註冊鍵盤升啟關閉訊息
- //鍵盤升起
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
- //鍵盤降下
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
“口袋郴州”項目總結-NSNotificationCenter