Pierre Prevalon Pierre Prevalon - 2 years ago 58
Swift Question

How can i create a segue that pass data to another view controller only if certain criteria are met

I want to create a segue to pass data to another view controller but there are certain criteria that must happen for the segue to happen. If possible i would prefer to use the segue Id instead of the dragging method.

this is an example Im trying to accomplish

@IBAction func SubmitButtonPressed(sender: AnyObject) {

if 1<0 {
// dont perform segue
//Perform segue

// i want to pass this data in the next VC
var data = "foo"

//this is my segue id i want o use to go to the Second VC
var segueId = "segueForgotPasswordTwo"

// second VC
var secondVc = "viewController2"

// Iwant to to use prepare for segue but im getting errors in the parameters
prepareForSegue(UIStoryboardSegue, sender: AnyObject?){



Answer Source

Your question is a bit unclear but I believe this is what you are looking for...

    func someFunction(){

        if //some condition {

        }else if //some condition {

        } else {
        //perform segue by using the folowing line. Assign the identifier to the segue in the storyboard.
            //Do this by first creating a segue by dragging from a view controller to the destination view controller. Be sure to drag from the VIEWCONTROLLER, to the destination VIEWCONTROLLER. DO NOT just drag from the button. Next, choose the type of segue (eg. show or present modally), and then type in an indentifier for this segue.
        performSegueWithIdentifier("SegueIdentifier", sender: nil)

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "SegueIdentifier" {

            //now find your view controller that you are seguing to.
            let controller = segue.destinationViewController as! SomeViewController

            //access the properties of your viewController and set them as desired. this is how you will pass data along
            controller.property = someValue

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download