ios 使用操作按钮更改视图控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26541302/
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
Change view controller using action button
提问by Mohammad Nurdin
How can I switch the view controller using UIButton? Here is my code:
如何使用UIButton切换视图控制器?这是我的代码:
@IBAction func scanAction(sender: AnyObject) {
//switch view controller
}
When I click the Scan button, it will go to a login form.
当我单击“扫描”按钮时,它将转到登录表单。
My view controller in Main.storyboardis like this
我在Main.storyboard 中的视图控制器是这样的
How might I proceed?
我该如何进行?
回答by Mohammad Nurdin
I already found the answer
我已经找到答案了
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
回答by Steve Rosenberg
One way is to just have a modal segue from a button. No IBOutlet required.
一种方法是从按钮中获得模态转场。不需要IBOutlet。
Programatically:
以编程方式:
@IBAction func scanButton (sender: UIButton!) {
performSegueWithIdentifier("nextView", sender: self)
}
You should add a modal segue and name the identifier. You connect the VC1 to VC2.
您应该添加一个模态转场并命名标识符。您将 VC1 连接到 VC2。
回答by Shaba Aafreen
The easiest way is to create a UINavigationViewController. Then add a Button to the current screen. Now press control and drag the Button to the Target View Controller. Thats it.
最简单的方法是创建一个 UINavigationViewController。然后在当前屏幕上添加一个 Button。现在按下 control 并将 Button 拖动到 Target View Controller。就是这样。
Source: iOs UINavigationViewController.
回答by Samuel
There is actually an answer without hardcoded code. In your storyboard, you can control drag the button to the next view controller and define the segue there. This will ensure that whenever you press the button, the segue will trigger. You can see this in the button's "Connections Inspector" at the triggered segues.
实际上有一个没有硬编码代码的答案。在您的故事板中,您可以控制将按钮拖到下一个视图控制器并在那里定义转场。这将确保每当您按下按钮时,segue 都会触发。您可以在触发的 segue 处的按钮的“连接检查器”中看到这一点。
If you want to do put data in the destination view controller, you can add an inaction to the button and put the data in prepare for segue function. The cool thing about this is that your triggered segues will still trigger from your button. This part would look like this:
如果你想把数据放在目标视图控制器中,你可以在按钮上添加一个不动作并将数据放在准备转场功能中。很酷的一点是你触发的 segue 仍然会从你的按钮触发。这部分看起来像这样:
@IBAction func buttonPressed(_ sender: UIButton) {
someImportantData = "some data if needed"
//no need to trigger segue :)
}
//not your case, but in order to understand the sage of this approach
@IBAction func button2Pressed(_ sender: UIButton) {
someImportantData = "some data2 if needed"
//no need to trigger segue :)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//retrieve the destination view controller for free
if let myDestincationViewController = (segue.destination as? MyDestincationViewController) {
myDestincationViewController.someImportantData = someImportantData
}
}
This way you do not need any hardcoded strings for segue identifiers, for storyboard identifiers, etc. and you can even prepare your destination view controller if needed.
通过这种方式,您不需要任何用于 segue 标识符、故事板标识符等的硬编码字符串,如果需要,您甚至可以准备目标视图控制器。