Search code examples
iosios6uiviewcontrollerstoryboard

How can I dismiss view controller or unwind from it from NotificationCenter observer?


I'm new to iOS development so I feel like I'm missing some fundamental knowledge here.

I have view controller (VC) that displays some information that comes form the server. My VC has two notification center observers - one for successful response from server and one for error.

I added observers like this

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getPlaceInfoFailed:)
                                                 name:kGetPlaceInfoRequestDidFailBlockNotification
                                               object:nil];

In case of error I want to display alert and navigate to previous view controller. Let's name them view controllers A and B.

I'm using storyboards.

My problem is that when I return to A UI is completely messed up - navigation bar has random stuff, my table view has garbage and etc. Eventually app crashes.

Here is how I tried to dismiss my VC B

- (void)getPlaceInfoFailed:(NSNotification *)notification {
dispatch_async(dispatch_get_main_queue(), ^{
        //[self.navigationController popViewControllerAnimated:YES];
        //[self dismissViewControllerAnimated: YES completion: nil];
        //[self.presentingViewController dismissViewControllerAnimated: YES completion: nil];
        //[self.presentedViewController dismissViewControllerAnimated: YES completion: nil];

        //[self performSegueWithIdentifier:@"exitSegue" sender:self];
    });
}

Version [self performSegueWithIdentifier:@"exitSegue" sender:self]; works when I invoke it, for example, in button action handler.

But not in observer :( As you can see I tried to invoke performSegue in UI thread - no difference.

What am I doing wrong ?

Thank you!!!


Solution

  • Looks like my problem was that I was calling performSegueWithIdentifier before viewDidAppear and that why it didn't work.

    How I can handle two asynchronous events - view creation and requesting server I don't know yet.