【发布时间】:2011-06-09 18:59:12
【问题描述】:
所以,我只是在各种情况下测试 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
【问题讨论】:
-
请贴代码,你的问题太长,难以理解。
-
请告诉我哪个部分难以理解,我会尽力编辑并回帖。
-
奇怪的是,只有某些通知会触发。你是如何设置观察者的?
-
我的工作正常。我不确定你是如何设置的,但我有一个工作项目。我会将代码发布在答案中,以便您查看
-
对不起,但我仍然没有得到你的日志和操作顺序,没有太多代码,我无法计算何时发布通知,以及应该何时收到通知。这可能是 sequencing 动作的问题。另一个潜在的错误来源是没有通过在控制器上保留保留来释放控制器。尝试将您的问题简化为一个真正简单的代码,并将其与所有相应的日志一起发布:)
标签: iphone objective-c cocoa-touch nslog nsnotifications