NSNotificationCenter:可以用來處理兩個數據傳遞(兩個文件之間,如兩個ViewController或者一個AppDelegate和ViewController文件之間)
方法一:
第一步:設置併發送通知,其中『NotificationName』爲該通知的唯一標識,(以後可以通過這個name找回這個通知),『object』爲通知所帶的內容,可以爲任意的NSObject型數據;
[[NSNotificationCenter defaultCenter] postNotificationName:@"pushAPNS" object:dic];//發送一個字典過去
第二步:接受數據,在你的相應函數裏設置接受通知代碼,如果是別的頁面傳數據過來,則在該文件的viewDidLoad函數中添加下列代碼,其中@Selector()裏的是處理函數,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addPointAnnotationForAPNS:) name:@"pushAPNS" object:nil];
處理函數:
- (void)addPointAnnotationForAPNS:(NSNotification *) notification {
NSDictionary *dic = [notification object];//dic裏的就是之前傳過來的NSObject數據
...//數據處理
}
第三步:dealloc通知,在dealloc方法中添加一下代碼即可
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"pushAPNS" object:nil];