iOS開發之通知中心

iOS開發過程中,一定要涉及到通知中心和觀察者兩種開發模式。

現在講明一些通知中心的簡單應用。



//通知中心接收事件,儘量有添加就要有刪除,object是相互傳遞的參數

- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"test" object:nil]; 

} - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; 

[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil]; 

}


//想通知中心發送事件,觸發通知中心想要監控事件的調用方法

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil];

//監控鍵盤彈出事件

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

    

    [nc addObserver:self selector:@selector(keyboardShowUp —> 自定義事件 ) name:UIKeyboardWillShowNotification object:nil];

    

    [nc addObserver:self selector:@selector(keyboardHide —> 自定義事件 ) name:UIKeyboardWillHideNotification object:nil];


本文有因爲問題請聯繫

QQ:563699115

Telephone:18341266547



發佈了28 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章