Question

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!

Was it helpful?

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.

OTHER TIPS

did you try adding the observer in the init method

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(testSetupFunction) name:@"testSetup" object:nil];                   

        // Custom initialization
    }
    return self;
}

OR You can also try to put a log in viewDidLoad to check actually the view is loaded at that time, sometimes it takes time to load the view, view is only loaded only when it is needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top