简体   繁体   中英

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

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 

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 .

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)

In Swift 3/4

     let storyB = UIStoryboard(name: "Main", bundle: nil) 
     let secondViewController = storyB.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
     self.present(secondViewController, animated: true, completion: nil)
 let storyB = UIStoryboard(name: "Main", bundle: nil) 
 let secondViewController = 
 storyB.instantiateViewController(withIdentifier: 
 "SecondViewControllerID") as! SecondViewController
 self.present(secondViewController, animated: true, completion: nil)

In the secondViewController use this code to go back.

 self.dismiss(animated: true, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM