0

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
trungduc
  • 11,926
  • 4
  • 31
  • 55
Hanz Cheah
  • 761
  • 5
  • 15
  • 44

2 Answers2

2

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];
}
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • When I first launch the app and I click on btnSend, it goes to SecondViewController but the `addObserver` or `receiveTestNotification:` is not triggered. I have to click the FirstVC tab and click the `btnSend` again, then only everything function accordingly? – Hanz Cheah Apr 18 '18 at 06:21
  • How can it triggered if you don't click on `btnSend`? – trungduc Apr 18 '18 at 06:35
  • When the app finish launching, I click on btnSend, it doesn't work. It only direct to SecondVC. Then when I click the tab back to FirstVC and click btnSend again, then only it work? – Hanz Cheah Apr 18 '18 at 07:14
  • It's because you call `postNotificationName` before change tab or I can say that `postNotificationName` is called before `SecondViewController` is initialized. To make it work, swap 2 like in `- (IBAction)btnSend:(id)sender` method – trungduc Apr 18 '18 at 07:18
  • @HansheungCheah Updated my answer – trungduc Apr 18 '18 at 07:20
  • You are a genius! Thank you very much. – Hanz Cheah Apr 18 '18 at 07:26
  • Welcome my friend ;) – trungduc Apr 18 '18 at 07:26
  • Sorry to disturb you, if I want to pass string is it `NSDictionary *userInfo = [NSDictionary dictionaryWithObject:sURL forKey:@"theURL"];` – Hanz Cheah Apr 18 '18 at 07:28
  • And receiving side `NSDictionary* userInfo = notification.userInfo; NSString* sFromSearch = (NSString*)userInfo[@"theRemain"];` – Hanz Cheah Apr 18 '18 at 07:29
  • I think you should pass it to `postNotificationName` method. Instead of `self`, pass string you want – trungduc Apr 18 '18 at 07:45
  • Now when I use it at my real code, it doesn't load when btn is clicked initially again. Will post a new questions. The passing of data works. – Hanz Cheah Apr 18 '18 at 08:09
  • You should check `viewControllers` are created or not at this time. I think one of these `viewControllers` isn't initialized. – trungduc Apr 18 '18 at 08:10
0
// 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];
 }
Nayab Khan
  • 21
  • 6