0

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!

daspianist
  • 5,336
  • 8
  • 50
  • 94
  • Have you stepped through the viewDidLoad method and made sure that the line posting the notification is actually reached? – rocky Oct 16 '13 at 23:39
  • Good suggestion - I did step through the original method posted in the ViewController and the `viewDidLoad` method in the segued view controller. In both cases the line posting and listening for the notification were reached. – daspianist Oct 17 '13 at 01:46

2 Answers2

3

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.

Kevin
  • 16,696
  • 7
  • 51
  • 68
  • This did the trick - thanks! What was strange was that even when I put a delay of `0.0` it still worked. However, if I called posted the notification in the original manner, it would not work.. Thanks for this workaround! – daspianist Oct 17 '13 at 01:51
  • 1
    This also works for me, but not when I put a delay of 0.0. I wonder if there's an explanation to this. – tyegah123 Jan 21 '14 at 03:24
  • Me to, neede that delay – JSA986 Jan 21 '14 at 12:27
0

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.