所以,我只是在各種情況下測試NSNotifications,這是令人困惑的。如果你能幫助我理解NSNotifications,我將不勝感激!視圖控制器有時不會收到一個NSNotification
我有一個導航控制器。
我有一個名爲「添加」的UIBarButtonItem,
如果我點擊添加它推我view2的哪些帖子通知DidAddNotification。
// I add view2 as observer and write method for this and NSlog if it gets implemented //
我再強迫自己查看3
// I add view3 as another observer and use the same method as the previous view and I NSlog if it gets implemented//
從View 3,我popToRootViewControllerAnimated:是,我回到1,再次遵循相同的程序。
因此,這是怎麼控制的?
1 -> 2 -> 3 -> 1
if I press add again,
the control is again the same 1 -> 2-> 3-> 1
下面是輸出(NSLogs):我按添加首次
:
2011-06-09 14:47:41.912 Tab[5124:207] I am the notification in view2
2011-06-09 14:47:41.912 Tab[5124:207] I pressed Add Button and I just sent a notification from view 1
// No notification in view 3 ?? // I am now back to view 1.
我再次按Add:
2011-06-09 14:47:51.950 Tab[5124:207] I am the notification in view3
2011-06-09 14:47:51.951 Tab[5124:207] I pressed Add Button and I just sent a notification from view 1
// No Notification in view 2 ??? // ... I am now back to view 1.
我按添加更多的時間:
2011-06-09 14:47:59.160 Tab[5124:207] I am the notification in view 3
2011-06-09 14:47:59.161 Tab[5124:207] I pressed Add Button and I just sent a notification from view 1
// No Notification in view 2 ??? // ... I am now back to view 1.
And this goes on..
誰能告訴我,爲什麼
- 的NSLog鑑於3未打印的第一次,但打印的所有其他時間?
- 爲什麼NSLog首次在視圖2中打印並且不再打印它?
代碼:
[[NSNotificationCenter defaultCenter] postNotificationName:@"DidAddNotification" object:self]; // I put this in the - (IBAction) for addData
- (void)didPressAdd:(NSNotification *)notification { //NSLogs// }
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPressAdd:) name:@"DidAddNotification" object:nil]; // I put this in the viewDidLoad of view 1 and view 2
請張貼代碼,您的問題太長,難以理解。 – 2011-06-09 19:04:33
請告訴我哪一部分很難理解,我會盡我所能編輯併發回。 – Legolas 2011-06-09 19:09:53
奇怪的是,只有某些通知會觸發。你如何設置觀察員? – justin 2011-06-09 19:32:01