视图控制器有时不会收到NSNotification [英] View controllers sometimes do not receive an NSNotification

查看:98
本文介绍了视图控制器有时不会收到NSNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我只是测试NSNotifications在各种情况下,这一个是混乱。如果你能帮助我理解NSNotifications,我将非常感激。



我有一个导航控制器。



我有一个名为Add的UIBarButtonItem,它发布通知DidAddNotification



如果我单击添加它推送我到view2。

  //我添加view2作为观察者和写方法和NSlog如果实现// 



我再次推自己查看3.

  //我添加view3作为另一个观察者,并使用与上一个视图相同的方法和NSlog如果它被实现// 

从视图3,我popToRootViewControllerAnimated:YES,我回到1,并再次按照相同的过程。



如何控制...

  1  - > 2  - > 3  - > 1 

如果我再次按下添加,

控制再次相同1 - > 2-> 3-> 1

以下是输出(NSLogs)



我第一次按添加:

  2011-06-09 14:47: 41.912标签[5124:207]我是在view2的通知
2011-06-09 14:47:41.912选项卡[5124:207]我按下添加按钮,我刚从视图1发送通知
//在视图3中没有通知//我现在回来查看1.

我再次按下添加:

  2011-06-09 14:47:51.950标签[5124:207]我是view3中的通知
2011-06-09 14 :47:51.951标签[5124:207]我按了添加按钮,我刚从视图1发送通知
//没有通知在视图2? // ...我现在回来查看1.

我按一次再添加一次: / p>

  2011-06-09 14:47:59.160标签[5124:207]我是视图3中的通知
2011-06-09 14:47:59.161标签[5124:207]我按了添加按钮,我刚刚发送通知从视图1

//没有通知在视图2? // ...我现在回到查看1.


这是继续..

任何人都可以告诉我为什么


  1. NSLog首次未在视图3中打印,














$ b

代码:

  [[NSNotificationCenter defaultCenter] postNotificationName:@DidAddNotificationobject:self]; //我把这个放在 - (IBAction)的addData 

- (void)didPressAdd:(NSNotification *)notification {// NSLogs //}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didPressAdd :) name:@DidAddNotificationobject:nil]; //我把它放在视图1和视图2的viewDidLoad中


解决方案

当你第一次发送通知,其他视图控制器不存在,它们还没有创建。viewController仍然是nil。由于没有观察者对象,你不会得到任何日志。第二次,视图控制器中的两个对象都已创建,因此他们在活动时收到通知,并记录收到的通知语句。


So, I am just testing NSNotifications on a variety of cases and this one is confusing. I would appreciate it if you could help me understand NSNotifications !

I have a Navigation Controller.

I have a UIBarButtonItem called "Add", which posts notification DidAddNotification

If I click Add it pushes me to view2.

 // I add view2 as observer and write method for this and NSlog if it gets implemented //

I again push myself to view 3.

// I add view3 as another observer and use the same method as the previous view and I NSlog if it gets implemented//

From View 3, I popToRootViewControllerAnimated:YES and I get back to 1. and again follow the same procedure.

So this is how the control is ...

1 -> 2 -> 3 -> 1

if I press add again,

the control is again the same 1 -> 2-> 3-> 1

Here's the output (NSLogs) :

I press Add for the first time:

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.

I press Add again:

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.

I press Add one more time:

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..

Could anyone tell me why

  1. NSLog did not print in view 3 for the first time but prints all other time?
  2. Why NSLog prints in view 2 for the first time and never prints it again ?

Code:

[[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

解决方案

"The first time when you send the notification, other view controllers don't exist. They haven't been created yet. The viewController is still nil yet. Since there is no observer object, you don't get any logs. Second time around, both objects in the view controllers have been created. So they receive the notification as they are alive and the log the received notification statements."

这篇关于视图控制器有时不会收到NSNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆