2011-06-09 35 views
0

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

谁能告诉我,为什么

  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 
+1

请张贴代码,您的问题太长,难以理解。 – 2011-06-09 19:04:33

+0

请告诉我哪一部分很难理解,我会尽我所能编辑并发回。 – Legolas 2011-06-09 19:09:53

+0

奇怪的是,只有某些通知会触发。你如何设置观察员? – justin 2011-06-09 19:32:01

回答

1

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

3

你所描述的似乎是差异归因于哪些对象是活着的时候的变化。视图和视图控制器不会无限期地存在,并不是在应用程序启动时全部创建。一个对象必须存在才能接收和记录通知。基本通知系统按预期工作。

如果您添加日志语句以宣告何时应创建一个应接收这些通知中的一个的对象,并且该对象在-init(或)主体内被销毁时,您应该能够看到生命周期对收到的消息的影响无论你的超类的指定初始值是什么)和-dealloc

另外:如果使用日志记录功能(例如NSLog(@"%s: <message>", __func__))标记日志语句,您的日志语句将更容易追踪。编译器为每个包含该函数名称的函数生成一个名为__func__的字符串。

1

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

#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]; 
} 

而且说实话,这几乎是它。在我的第三课,我弹出到根控制器,而不是创建一个新的视图控制器,但就是这样。它会告诉你每次点击该按钮后你会看到哪个视图,所以希望它能帮助你更好地理解通知的工作方式。 “

+0

非常感谢时间花花公子。我不知道为什么这发生在我身上。我知道这不可能是原因 - (大声笑),但让我问你这个...你使用哪种Xcode?我正在使用ios5 sdk beta的最新版本。 – Legolas 2011-06-09 20:45:55

+0

哈哈,我不知道你的情况怎么样。我在iOS 4.3中使用Xcode 3,所以这可能是原因。我有点怀疑,但你永远不知道。根据WWDC的说法,我知道他们改变了通知系统,但我认为这与NSNotificationCenter没有任何关系,但你永远不知道 – justin 2011-06-09 20:53:33

+0

那么。它们的可能性非常低,它们会在ios5 sdk中改变这种情况。大声笑。如果我把这个放在developer.apple.com上,问他们他们会有可能发生LOL。我会再坐下来分析一下代码....(叹气)!感谢帮助伙计。 – Legolas 2011-06-09 20:57:21

相关问题