How can I switch the view controller using UIButton? Here is my code:
@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.
I already found the answer
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.
Source: iOs UINavigationViewController.
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.
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:
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.
One way is to just have a modal segue from a button. No IBOutlet required.
Programatically:
You should add a modal segue and name the identifier. You connect the VC1 to VC2.