Hanz Cheah
Hanz Cheah

Reputation: 811

NSNotificationCenter not working, how to debug

I followed the dreammlax example, but I can't seem to get my NSNotification to work. Is there a framework or something I need to add? How do I debug the code.

FirstViewController posting

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (IBAction)btnSend:(id)sender {

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" 
          object:self];

    self.tabBarController.selectedIndex = 1;

}

@end

SecondViewController receiving

 #import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"TestNotification"
                                               object:nil];

   //===Removed ===[[NSNotificationCenter defaultCenter] removeObserver:self];

}

- (void) receiveTestNotification:(NSNotification *) notification
{

    if ([[notification name] isEqualToString:@"TestNotification"])
    NSLog (@"Successfully received the test notification!");

}

@end

Upvotes: 0

Views: 1840

Answers (2)

Nayab Khan
Nayab Khan

Reputation: 31

// from where u want to post notification  

- (IBAction)PressedMeBtn:(id)sender {

KNcreateOfferBack is define string!!!!

[[NSNotificationCenter defaultCenter] postNotificationName:KNcreateOfferBack object:nil];

}


//And where you are sending notification and observer 

- (void)BackFromControllers:(NSNotification *)note {
NSLog(@"Received Notification Inside LeftMenu - event");

if([[note name] isEqualToString:KNcreateOfferBack]){

    NSLog(@"KNcreateOfferBack NoteName :***: %@ :***:",note.name);
    // done your work here and then remove your notification !!!!
[[NSNotificationCenter defaultCenter] removeObserver:self name:KNcreateOfferBack object:nil];

 }

// use observer in viewWillAppear where you are using above method

 - (void)viewWillAppear:(BOOL)animated {


[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(BackFromControllers:)
                                             name:KNcreateOfferBack object:nil];
 }

Upvotes: 0

trungduc
trungduc

Reputation: 12144

Remove [[NSNotificationCenter defaultCenter] removeObserver:self]; after adding observer and it will work

Or you can move [[NSNotificationCenter defaultCenter] removeObserver:self]; to dealloc method

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

For the question why it doesn't work on first time run.

It's because postNotificationName is called before SecondViewController is initialized. To fix it try the below code.

- (IBAction)btnSend:(id)sender {
    self.tabBarController.selectedIndex = 1;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"TestNotification" 
      object:self];
}

Upvotes: 2

Related Questions