所以,我只是在各种情况下测试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