Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Successive calls to UIViewController's presentViewController method

I recently encountered a hair-pulling situation in my iOS app, where I was trying to successively dismiss one presented UIViewController from my window's rootViewController, using:

[rootViewController dismissViewControllerAnimated:YES completion:NULL]

and present another one shortly thereafter (in another method, incidentally), with:

UIViewController *vc2 = [[[MyViewController2 alloc] initWithNibName:nil bundle:nil] autorelease];
[rootViewController presentViewController:vc2 animated:YES completion:NULL];

Problem was, I could never get the second view controller to show up. Turns out, as near as I can tell, dismissViewControllerAnimated:completion: needs that asynchronous block of "completion" time to pass, before presentViewController:animated:completion: will work properly again. This fact is not directly documented in Apple's docs, from what I can tell.

The solution I came up with was to wrap the dismissal with a method that specifies the selector you would want to call afterwards, like so:

- (void)dismissViewController:(UIViewController *)presentingController
                   postAction:(SEL)postDismissalAction
{
    [presentingController dismissViewControllerAnimated:YES
                                             completion:^{
                                                            [self performSelectorOnMainThread:postDismissalAction
                                                                                   withObject:nil
                                                                                waitUntilDone:NO];
                                                         }];
}

And then I would call:

[self dismissViewController:self.window.rootViewController
                 postAction:@selector(methodForNextModalPresentation)];

Anyway, I wanted to post, as I looked around and hadn't seen anyone with this particular problem, so I thought it might be useful for people to understand. And also, I wanted to verify that I'm not hacking a solution that has a better design pattern for resolution.

like image 554
KevinH Avatar asked May 17 '12 02:05

KevinH


1 Answers

Just for the sake of clarity. are you saying that this code doesn't work?

[myRootViewController dismissViewControllerAnimated:YES completion:^{
    [myRootViewController pushViewController:newController animated:YES];
}];
like image 175
Valerio Avatar answered Oct 01 '22 20:10

Valerio