Chisx Chisx - 5 months ago 36
Objective-C Question

How can I programmatically present a view controller modally?

Edit: When solving this problem, I found that it is much easier to start with your

UITabBarController
, then perform login validation via your
AppDelegate.m
's
didFinishLaunchingWithOptions:
method.

Question:
This code is in the the
application didFinishLaunchingWithOptions:
method in my AppDelegate.m

if([result isEqualToString: @"log"])
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
[(UINavigationController*)self.window.rootViewController pushViewController:ivc animated:NO];
NSLog(@"It's hitting log");
}


It simply takes an HTTP response for the user being logged in, and takes them to my TabBarController. The problem is that it's using a push rather than a modal transition to display the page. Since presentModalViewController method is deprecated or deleted in iOS7, how can I programmatically force a modal presentation?

EDIT:
)

Answer

The old presentViewController:animated: method has been deprecated, we need to use presentViewController:animated:completion instead. You now simply have to add a completion parameter to the method - this should work:

if([result isEqualToString: @"log"])
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControl"];
    [(UINavigationController*)self.window.rootViewController presentViewController:ivc animated:NO completion:nil];
    NSLog(@"It's hitting log");
}

The docs are a good place to start - the docs for UIViewController presentViewController:animated tell you exactly what you need to know:

presentModalViewController:animated:

Presents a modal view managed by the given view controller to the user. (Deprecated in iOS 6.0. Use presentViewController:animated:completion: instead.)

- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated