【问题标题】:View controllers sometimes do not receive an NSNotification视图控制器有时不会收到 NSNotification
【发布时间】: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..

谁能告诉我为什么

  1. NSLog 不是第一次在视图 3 中打印,而是在所有其他时间都打印?
  2. 为什么 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


【解决方案1】:

您所描述的差异似乎是由于对象在何时处于活动状态的变化。视图和视图控制器不是无限存在的,也不是在应用启动时创建的。必须存在一个对象才能接收和记录通知。基本通知系统按预期工作。

如果您添加日志语句来宣布应该接收这些通知之一的对象何时创建以及何时在-init 的正文中被销毁,您应该能够看到生命周期对收到的消息的影响(或者你的超类的指定初始化器是什么)和-dealloc

另外:如果你用像NSLog(@"%s: <message>", __func__)这样的函数来标记它们,你的日志语句将更容易追踪。编译器为每个包含函数名称的函数生成一个名为__func__ 的字符串。

【讨论】:

    【解决方案2】:

    我刚刚设置了一个基于导航的应用程序。在根控制器标头中,我有这个:

    #import <UIKit/UIKit.h>
    
    extern NSString * const EPNotification;
    
    @interface RootViewController : UITableViewController {
    }
    @end
    

    我真正不同的是设置了一个在整个代码中使用的字符串。然后在根实现文件中,我有这个(加上所有标准的东西):

    #import "RootViewController.h"
    #import "One.h"
    
    NSString *const EPNotification = @"Notification"; // this will be the global string name for the notification
    
    @implementation RootViewController
    
    
    #pragma mark -
    #pragma mark View lifecycle
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotNotification:) name:EPNotification
                                               object:nil];
    
        UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain                                                          target:self action:@selector(sendNotification)];
    
        self.navigationItem.rightBarButtonItem = next;
        [next release];
    }
    
    - (void)sendNotification {
        NSDictionary *d = [NSDictionary dictionaryWithObject:@"view 1" forKey:@"sender"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self userInfo:d];
        One *o = [[One alloc] initWithNibName:@"One" bundle:nil];
        [self.navigationController pushViewController:o animated:YES];
        [o release];
    }
    
    - (void)gotNotification:(NSNotification *)note {
        NSLog(@"from %@", [[note userInfo] objectForKey:@"sender"]);
    }
    

    我还有 3 个其他视图(分别为一、二和三),它们几乎完全相同。标题中没有任何内容(除了标准内容)。我将发布其中一个 .m 文件,以便您查看设置。

    #import "One.h"
    #import "Two.h"
    
    @implementation One
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain
                                                         target:self action:@selector(sendNotification)];
    
        self.navigationItem.rightBarButtonItem = next;
        [next release];
    }
    
    - (void)sendNotification {
        NSDictionary *d = [NSDictionary dictionaryWithObject:@"view 2" forKey:@"sender"];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Notification" object:self userInfo:d];
        Two *t = [[Two alloc] initWithNibName:@"Two" bundle:nil];
        [self.navigationController pushViewController:t animated:YES];
        [t release];
    }
    

    老实说,差不多就是这样。在我的第三课上,我弹出到根控制器而不是创建一个新的视图控制器,但就是这样。每次单击按钮时,它都会告诉您当前处于哪个视图,因此希望它能帮助您更好地了解通知的工作原理。

    【讨论】:

    • 非常感谢您抽出宝贵的时间。我不知道为什么这会发生在我身上。我知道这不是原因 - (哈哈)但是让我问你这个......你使用的是哪个 Xcode?我正在使用带有 ios5 sdk beta 的最新版本。
    • 哈哈,我也不知道你的情况如何。我将 Xcode 3 与 iOS 4.3 一起使用,这可能就是原因。我有点怀疑,但你永远不知道。根据 WWDC,我知道他们更改了通知系统,但我认为这与 NSNotificationCenter 无关,但你永远不知道
    • 所以。他们在 ios5 sdk 中更改类似内容的可能性极低。哈哈。如果我把它放在 developer.apple.com 上并问他们,他们可能会大声笑。我会坐下来再次分析代码....(叹气)!谢谢老哥的帮助。
    • 我的荣幸。糟透了,它仍然像那样和你在一起。我会去查看 iOS5 的 NSNotificationCenter 类参考资料,看看是否有任何更改。此外,我可能会注册开发者计划(而不是我永远拥有的小免费帐户),因为我的应用程序已经非常接近准备发布。所以如果我这样做了,我会查看通知内容,看看我是否也可以让它在那个 iOS 版本上运行,并让你知道。与此同时,我很乐意尽我所能提供帮助
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2012-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      相关资源
      最近更新 更多