Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically present a view controller modally?

Tags:

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: )

like image 477
Chisx Avatar asked Jan 02 '14 22:01

Chisx


People also ask

How do you call a view controller in storyboard programmatically?

NSString * storyboardName = @"MainStoryboard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; [self presentViewController:vc animated:YES completion:nil];

How do I embed a view controller?

Open Main. storyboard and select the view controller of the scene that is already present. Open the Identity Inspector on the right and set Class to MasterViewController in the Custom Class section. With the view controller selected, choose Embed In > Navigation Controller from the Editor menu.


1 Answers

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 
like image 193
Undo Avatar answered Sep 23 '22 23:09

Undo