Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change view controller using action button

Tags:

ios

swift

xcode6

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

enter image description here

How might I proceed?

like image 241
Nurdin Avatar asked Oct 24 '14 04:10

Nurdin


People also ask

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.


2 Answers

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)
like image 176
Nurdin Avatar answered Nov 09 '22 07:11

Nurdin


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

enter image description here

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.

like image 33
Steve Rosenberg Avatar answered Nov 09 '22 06:11

Steve Rosenberg