所以,我只是在各种情况下测试 NSNotifications,而这一个令人困惑。如果您能帮助我理解 NSNotifications,我将不胜感激!
我有一个导航控制器。
我有一个名为“Add”的 UIBarButtonItem,它发布通知 DidAddNotification
如果我单击添加,它会将我推到 view2。
// 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//
从视图 3 中,我 popToRootViewControllerAnimated:YES 并返回到 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.
我再次按添加:
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