View controllers sometimes do not receive NSNotification

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

I have a navigation controller.

I have a UIBarButtonItem called "Add" that sends a DidAddNotification notification

If I click Add, this will push me to view2.

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

I push myself again 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 will return to 1. and perform the same procedure again.

So this is management ...

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 click "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 click the Add button 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 click "Add" again:

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

Can anyone tell me why

  • 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
0
3

" , , . . . - , . , , , - ."

+1

, , -, , , . . . .

, , , , , -init ( ) -dealloc.

: , , , NSLog(@"%s: <message>", __func__). __func__ , .

+3

. :

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

And honestly, this is pretty much the case. In my class three, I go out to the root controller instead of creating a new view controller, but it is. It tells you what kind of view you work with every click of a button, so hopefully this will help you better understand how notifications work.

+1
source

All Articles