ios 如何以编程方式以模式方式呈现视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20893505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I programmatically present a view controller modally?
提问by Chisx
Edit:When solving this problem, I found that it is much easier to start with your UITabBarController
, then perform login validation viayour AppDelegate.m
's didFinishLaunchingWithOptions:
method.
编辑:在解决这个问题时,我发现从你的 开始UITabBarController
,然后通过你AppDelegate.m
的didFinishLaunchingWithOptions:
方法执行登录验证要容易得多。
Question:This code is in the the application didFinishLaunchingWithOptions:
method in my AppDelegate.m
问题:此代码application didFinishLaunchingWithOptions:
在我的 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 pushrather than a modaltransition to display the page. Since presentModalViewController method is deprecated or deleted in iOS7, how can I programmatically force a modal presentation?
它只是为正在登录的用户获取 HTTP 响应,并将它们带到我的 TabBarController。问题在于它使用推送而不是模式转换来显示页面。由于presentModalViewController 方法在iOS7 中已被弃用或删除,如何以编程方式强制进行模态演示?
回答by Undo
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:
旧presentViewController:animated:
方法已被弃用,我们需要presentViewController:animated:completion
改用。您现在只需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
告诉您确切需要知道的内容:
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
presentModalViewController:动画:
向用户呈现由给定视图控制器管理的模态视图。(在 iOS 6.0 中已弃用。
presentViewController:animated:completion:
改用。)- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
回答by Suragch
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.
由于接受的答案是在 Objective-C 中,这就是您在 Swift 中的做法。但是,与已接受的答案不同,此答案并未引用导航控制器。
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.
另请参阅如何以编程方式关闭视图控制器。
回答by Shahzaib Maqbool
In swift 4.2 you can do it like this. For those who want this answer in swift updated version.
在 swift 4.2 中,你可以这样做。对于那些想要快速更新版本的答案的人。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ExampleViewController")
self.present(controller, animated: true, completion: nil)
回答by Sh_Khan
In Swift 3/4
在斯威夫特 3/4
let storyB = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyB.instantiateViewController(withIdentifier: "SecondViewControllerID") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)
回答by Gregory Wilson Pullyattu
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)