Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically changing View Controllers without Navigation Controller Swift

I want to switch to a new view controller when I press a button without using a navigation controller and I can't seem to be able to do this. I have looked everywhere for an answer but everything that I have found is either in objective-c, using a navigation controller, or not working at all.

func gunsoutButtonPressed(sender:UIButton!) {
    print("yay\t")
    //let encounterVC:AnyObject! = self.storyboard?.instantiateViewControllerWithIdentifier("ecounterViewController")
    //self.showViewController(encounterVC as UIViewController, sender: encounterVC)
    let encounterViewController = self.storyboard.instantiateViewControllerWithIdentifier("encounterViewController") as encounterViewController
    self.pushViewController(encounterViewController, animated: true)
}
like image 584
samando Avatar asked Mar 18 '15 20:03

samando


2 Answers

You have to connect the two ViewControllers you would like to use. Then name the segue however you want and then use the following code to trigger the segue (inside an IBAction or something). It is not completely programmatically but you can trigger them programmatically, which should be enough.

performSegueWithIdentifier("mySegue", sender: nil)

Check out this video for support.

Hope this helps :)

like image 167
LinusGeffarth Avatar answered Sep 21 '22 15:09

LinusGeffarth


You cannot use a push segue without a UINavigationController. You could achieve this with a modal segue, such as this:

self.presentViewController(encounterViewController, animated: true, completion: nil)
like image 40
Matt Cooper Avatar answered Sep 21 '22 15:09

Matt Cooper