0

I am trying to pass NSString from one class to another. Let's say I have ViewController A and ViewController B. I want to pass NSString from A to B.

In ViewController A, I have following code :

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:userType];

//Here user type is a string I get using delegate and I need to pass this userType to ViewController B

In ViewController B, I have following code : In viewDidLoad , I have following code :

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

//This NSNotificationCenter method is called

I have registered the following selector method.

-(void) notificationAction:(NSNotification *) notification
{
    if ([notification.object isKindOfClass:[NSString class]])
    {
        NSString *message = [notification object];
        // do stuff here with your message data
        NSLog(@"%@ is message",message);
    }
    else
    {
        NSLog(@"Error, object not recognised.");
    }
}

//The above selector method is never called.

I have read other similar stackoverflow answers but I have not been able to find any solutions regarding this.

Rosh
  • 169
  • 16
  • In order for the above notification selector to actually be added, ViewController B's viewDidLoad must have been called (AKA the view must be shown) – Hayden Holligan Dec 02 '15 at 18:28
  • Thanks for clarifying that @Hayden Holligan. I was in fact loading A before B. – Rosh Dec 02 '15 at 18:39
  • 1
    Although your code works, please, don't use notification object in this way. Object in this case is the sender of notification instance. Use userInfo to pass some information between objects. – Borys Verebskyi Dec 02 '15 at 20:52
  • In my case I created an object in local method, So the observer class dealloc during notification fire from another class. You can check this: https://stackoverflow.com/questions/67328439/nsnotificationcenter-addobserver-not-working-in-release-mode-but-works-in-debug – Ravi Apr 30 '21 at 06:24

2 Answers2

8

Your code and syntax is clearly correct. I'm guessing that it's a matter of object lifecycle. I would hypothesize that either of the following are true:

  • ViewController B doesn't actually exist as an object when the notification is posted

or

  • ViewController A is posting the notification before ViewController B has had a chance to register for it.

One way you can verify either of these is to add two breakpoints, one where the notification is posted, and one where the notification listener is registered. The breakpoint where the listener is registered should be hit before the notification is posted. If that happens, then verify that ViewController B is in fact an object in existence when the notification is posted (like it's not popped off the navigation stack or something).

Andy Obusek
  • 12,614
  • 4
  • 41
  • 62
  • Thanks @Andy Obusek, I didn't have proper concept of NSNotification, I had my ViewController A loaded before ViewController B, so ViewController B could not register for NSNotification but it is mandatory for me to load ViewController A before ViewController B, so I think NSNotification is out of context now since I cannot load B before A. I will probably use delegate as a workaround. Thanks – Rosh Dec 02 '15 at 18:38
  • So what would be the solution for this. For my app I would need the post controller presented before the observer controller is present. Would Delegates do the fix? or is there a different approach to this. – N. Der Feb 11 '19 at 01:49
-1

This is what you are looking for NSNotification not being sent when postNotificationName: called You must addObserver before postNotificationName

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

[[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationMessageEvent" object:nil];
Community
  • 1
  • 1
Thuc Pham
  • 153
  • 1
  • 11