ios 如何快速切换视图控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25375409/
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 to switch view controllers in swift?
提问by ThatHybrid
I'm trying to switch from one UIViewController to another using code. Right now I have,
我正在尝试使用代码从一个 UIViewController 切换到另一个。现在我有,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
所有这些都是将我带到黑屏而不是 ResultViewController。有什么建议?
回答by Vivek Sehrawat
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller and in the appropriate function type this:
与故事板。为第二个视图控制器创建一个 swift 文件 (SecondViewController.swift) 并在适当的函数中输入:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
没有故事板
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
回答by Mohammad Nurdin
You can try this one
你可以试试这个
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
确保设置了视图控制器标识符。
回答by Cyril
Swift 3
斯威夫特 3
To present a controller modally
以模态方式呈现控制器
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
显示控制器
self.show(vc, sender: self)
回答by Rashad
Try this:
尝试这个:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
希望这可以帮助.. :)
回答by William DeGroot
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
最简单的方法是通过右键单击您正在启动的视图并将蓝线拖到结果视图控制器来创建一个 segue,然后点击 show segue 选项。然后将segue的标识符设置为“segue”。现在在视图控制器中的任何位置添加此代码:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
这将触发将转到您的 resultViewController 的 segue
回答by Shaba Aafreen
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen. Make sure you are using a UINavigationViewController.
在 iOS 中切换屏幕的最简单方法是。在当前屏幕上添加一个按钮。然后按下 control 并将按钮拖动到目标屏幕。然后选择推送。当您点击按钮时,屏幕将切换到目标屏幕。确保您使用的是 UINavigationViewController。
Source: ios UINavigationController Tutorial.
回答by J. Pataraccs
Shaba's answer is a good start but requires the following so that you can control the segue from within your view controller:
Shaba 的回答是一个好的开始,但需要以下内容,以便您可以从视图控制器中控制 segue:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }