简体   繁体   English

如何以编程方式以模态方式呈现视图 controller?

[英]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.编辑:解决这个问题时,我发现从UITabBarController开始,然后通过AppDelegate.mdidFinishLaunchingWithOptions:方法执行登录验证要容易得多。

Question: This code is in the the application didFinishLaunchingWithOptions: method in my AppDelegate.m问题:此代码在我的 AppDelegate.m 中的application didFinishLaunchingWithOptions:方法中

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.它只需要一个 HTTP用户登录响应,并将它们带到我的 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?由于 presentModalViewController 方法已在 iOS7 中弃用或删除,我如何以编程方式强制进行模态显示?

EDIT:编辑: ) )

The old presentViewController:animated: method has been deprecated, we need to use presentViewController:animated:completion instead. 旧的presentViewController:animated:方法已被弃用,我们需要使用presentViewController:animated:completion来代替。 You now simply have to add a completion parameter to the method - this should work: 您现在只需要在方法中添加一个completion参数 - 这应该有效:

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: 文档是一个很好的起点 - UIViewController presentViewController:animated文档UIViewController presentViewController:animated告诉你你需要知道的内容:

presentModalViewController:animated: presentModalViewController:动画:

Presents a modal view managed by the given view controller to the user. 呈现由给定视图控制器管理给用户的模态视图。 (Deprecated in iOS 6.0. Use presentViewController:animated:completion: instead.) (在iOS 6.0中不推荐使用。使用presentViewController:animated:completion:而不是。)

 - (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. 由于接受的答案是在Objective-C中,因此您将在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. 根据需要更改故事板名称,视图控制器和ID。

See also how to dismiss a view controller programmatically . 另请参见如何以编程方式关闭视图控制器

In swift 4.2 you can do it like this. 在swift 4.2中你可以这样做。 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 在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. 在secondViewController中使用此代码返回。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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