Search code examples
iphoneiosios7nsnotificationcenternsnotification

NSNotificationCenter previously working in iOS 6.1 - iOS 7 issue?


I have the following snippet of code in viewDidLoad which I post a notification to be received by a different ViewController, and prior to iOS 7 (and XCode 5) this has worked:

if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
        NSLog(@"Current user exists and is logged in"); //current works, so I know that this if-statement is satisfied
        [self performSegueWithIdentifier:@"GoToRatingsView" sender:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"testSetup" object:nil]; //part in question
    }

And in the segued ViewController, I have the following:

(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testSetupFunction) name:@"testSetup" object:nil];
}

(void)testSetupFunction
{
    NSLog(@"This function executed");
}

Currently, testSetupFunction does not executed, which means the notification is not received. I am unsure whether its because I have segued to a different view and then posted the notification, or that this is something new with iOS 7, but currently the notification is no longer received.

Thanks for your help!


Solution

  • Post the notification once the viewcontroller has time to load.

    - (void)postNotificaiton
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"testSetup" object:nil];
    }
    
    if ([PFUser currentUser] && [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
        [self performSegueWithIdentifier:@"GoToRatingsView" sender:self];
        [self performSelector:@selector(postNotification) withObject:nil afterDelay:0.0];
    }
    

    You could also post a notification from your segued View Controller saying that it was loaded, and in response to that notification, post your testSetup notification.