5

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)
}
2
  • If you don't want a navigation controller why are you using pushViewController in your code? That's a navigation controller method. You need to read the "View Controller Programming Guide for iOS" to understand what you're doing. Commented Mar 18, 2015 at 21:03
  • I posted an answer below but I would recommend to do some research before asking here, there are tons of videos out there. Commented Mar 18, 2015 at 21:12

3 Answers 3

9

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 :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks man. That worked perfectly. I am in eternal debt to you for saving me.
But 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?
Sorry, 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.
9

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

0

Segue

So you use the Segue ID to make sure you segue correctly.

No Segue

I would suggest doing this instead though:

let vc = ViewControllerToSegueTo()
self.presentViewController(vc, animated: true, completion: nil)

It is slightly better if you do not want to use the storyboard.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.