使用动作按钮更改视图控制器

时间:2022-12-01 22:52:50

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.storyboard is like this

我的视图控制器。故事板是这样的

使用动作按钮更改视图控制器

Please give me some advice if you can. Thank you.

如果可以的话,请给我一些建议。谢谢你!

4 个解决方案

#1


13  

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)

#2


7  

One way is to just have a modal segue from a button. No IBOutlet required.

一种方法是从按钮中获取一个模态segue。不需要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.

您应该添加一个modal segue并命名标识符。将VC1连接到VC2。

#3


1  

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。然后向当前屏幕添加一个按钮。现在按control键并将按钮拖动到目标视图控制器。这是它。

Source: iOs UINavigationViewController.

来源:iOs UINavigationViewController。

#4


1  

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都会触发。您可以在触发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:

如果要在目标视图控制器中放置数据,可以在按钮中添加不作为,并将数据放入prepareforsegue函数。很酷的是你的触发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标识符、故事板标识符等使用任何硬编码字符串,甚至可以在需要时准备目标视图控制器。

#1


13  

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)

#2


7  

One way is to just have a modal segue from a button. No IBOutlet required.

一种方法是从按钮中获取一个模态segue。不需要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.

您应该添加一个modal segue并命名标识符。将VC1连接到VC2。

#3


1  

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。然后向当前屏幕添加一个按钮。现在按control键并将按钮拖动到目标视图控制器。这是它。

Source: iOs UINavigationViewController.

来源:iOs UINavigationViewController。

#4


1  

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都会触发。您可以在触发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:

如果要在目标视图控制器中放置数据,可以在按钮中添加不作为,并将数据放入prepareforsegue函数。很酷的是你的触发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标识符、故事板标识符等使用任何硬编码字符串,甚至可以在需要时准备目标视图控制器。