How can I programmatically present a view controller modally?
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
In swift 4.2 you can do it like this. For those who want this answer in swift updated version.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)
Swift
Since the accepted answer is in Objective-C, this is how you would do it in Swift. Unlike the accepted answer, though, this answer does not reference the navigation controller.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewController") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
Change the storyboard name, view controller, and id as needed.
See also how to dismiss a view controller programmatically.