Programmatically changing View Controllers without Navigation Controller Swift
Solution 1
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 :)
Solution 2
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)
Comments
-
samando about 4 years
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) }
-
samando about 9 yearsThanks man. That worked perfectly. I am in eternal debt to you for saving me.
-
Jasper almost 9 yearsBut even without writing anything programatically, the navigation is happening, once i draw the segue between two views - how do i ensure that it happens only programatically?
-
LinusGeffarth almost 9 yearsSorry, I do not completely know how that works. I just know you have to somehow set the destination ViewController, probably w/ its storyboard ID. Maybe that helps you. There should be something findable on the internet.