View controllers sometimes do not receive an NSNotification

前端 未结 3 1565
情深已故
情深已故 2021-01-28 00:22

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 N

3条回答
  •  礼貌的吻别
    2021-01-28 01:20

    I just set up a navigation-based app. In the root controller header, I've got this:

    #import 
    
    extern NSString * const EPNotification;
    
    @interface RootViewController : UITableViewController {
    }
    @end
    

    All I really did different there was set up a string to be used throughout the code. Then in the root implementation file, I have this (plus all the standard stuff):

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

    I've got 3 other views (One, Two and Three, respectively), that are pretty much all exactly the same. Nothing in the header (besides the standard stuff). I'll post one of the .m files so you can see the setup.

    #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, that's pretty much it. On my class Three, I pop to the root controller instead of create a new view controller, but that's it. It tells you which view you're on with each click of the button, so hopefully it'll help you understand better how notifications work.

提交回复
热议问题