Search code examples
objective-ciphonenotificationspresentviewcontroller

Notification does not work in Presentviewcontroller objective-c


I am displaying a UIAlertController when I receive a response from API. After the response, I want to call the notification of other view controller.

I added observer in view controller 1 as below:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateAthleteDictionaryNotification:) name:@"UpdateAthleteDictionary" object:self.dictProfile];

In view controller 2 my code is below:

UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:applicationName
                                 message:[jsonObject valueForKey:@"responseMessage"]
                                 preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* yesButton = [UIAlertAction
                                actionWithTitle:@"Ok"
                                style:UIAlertActionStyleDefault
                                handler:^(UIAlertAction * action) {
                                    dispatch_async(dispatch_get_main_queue(), ^{
                                        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateAthleteDictionary" object:self.dictProfile];
                                    });
                                    [self.navigationController popViewControllerAnimated:YES];
                                }];
    [alert addAction:yesButton];
    [self presentViewController:alert animated:YES completion:nil];

But it does not call. So please can you suggest me the solutions.


Solution

  • NSNotificationCenter will deliver your notification only in case if name and object are the same (source). If you want to receive notifications with any object attached you should pass nil as an object argument, in your case:

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