Search code examples
iosobjective-cuiviewcontrollerios9presentviewcontroller

dismissViewControllerAnimated not working in iOS 9


I have a UIViewController subclass called NewsViewController which has a completion block property that is called from a button action. The controller is set up and presented in another view controller like this:

newsViewController.completionBlock = ^{
    [self dismissViewControllerAnimated:YES completion:nil];
};

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:newsViewController];
[self presentViewController:navigationController animated:YES completion:nil];

In iOS 10 this all works fine, however in iOS 9 the view is not being dismissed. If I put a breakpoint there it does get hit.

I have tried the following without success:

Called it from the main thread (both synchronously and asynchronously)

I have tried it using GCD like this:

dispatch_async(dispatch_get_main_queue(), ^{
    [self dismissViewControllerAnimated:YES completion:nil];
});

I have also tried it by putting the dismissal call into a method and then calling

[self performSelectorOnMainThread:@selector(dismissModalView) withObject:nil waitUntilDone:NO];

I don't actually thing the issue is the thread since a call to [NSThread isMainThread] from within the completion block returns YES.

Calling it with a delay

[self performSelector:@selector(dismissModalView) withObject:nil afterDelay:0.1];

Calling dismiss on another view controller

I have tried calling it on navigationController, self.presentedViewController and self.presentingViewController.

Calling dismiss directly from NewsViewController

In the button action where the completion block was called I called [self dismissViewControllerAnimated:YES completion:nil] directly.

Btw. just for fun I tried calling the dismiss method from the completion block of the presentViewController method and there it did get dismissed.


Solution

  • I have finally located the problem and it was quite unexpected. The thing is that the NewsViewController is presented over my login view controller. This controller allows the user to use Touch ID to log in so it requests the Touch ID prompt in its viewDidAppear method. Apparently, this messes with the dismissal of the presented view, and seemingly only in iOS 9 (well, maybe not only, but it seems to work fine in iOS 10).