Gonzalo Duarte
Gonzalo Duarte

Reputation: 45

How can I create a segue programmatically to pass info between view controllers - Swift

I'm new in swift, and I haven't found any answers about this. I have an game, where there's a slider, and a random value, so the user has to guess the value on the slider. When the user guesses the number, and presses the ok button it calls a view controller. When he gets pretty close, it calls another view controller, and finally if he doesn't guess it, it calls another one and the user restarts. My question is, how can I create a segue programmatically in order to pass the points earned and the rounds to each of the view controllers by just pressing ok. Those view controllers are created in the storyboard, and I call them by code

Thanks for your help!

@IBAction func okButton(sender: AnyObject) {

    let diferencia = abs(targetValue - currentValue)
    var point = 100 - diferencia
    score += point

    func bonusPoints() {
    score += 100
    }
    if diferencia == 0 {

        let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonPerfectViewController") as UIViewController
        presentViewController(viewController3, animated: true, completion: nil)
        point += 100
        newRound()
        updateLabel()
        bonusPoints()
            } else if diferencia < 4 {

            let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonNiceTry1ViewController") as UIViewController
            presentViewController(viewController3, animated: true, completion: nil)
            point += 50
            newRound()
            updateLabel()

             } else {

        let viewController3 = self.storyboard!.instantiateViewControllerWithIdentifier("GonThirdViewController") as UIViewController
        presentViewController(viewController3, animated: true, completion: nil)

        startnewgame()
        updateLabel()

    }
}

Upvotes: 2

Views: 7480

Answers (4)

Henning
Henning

Reputation: 119

And with Swift 3, developing on Linus G's answer which was a rewrite of Nirav Gadhiya's answer:

let storyboardName = "Main"
let viewControllerID = "id_of_viewcontroller_specified_by_you_in_storyboard"
let storyboard = UIStoryboard(name: storyboardName, bundle:nil)
let controller = storyboard.instantiateViewController(withIdentifier: viewControllerID)
controller.infoDict = myDictToPass
//If it is a modal view:
//controller.modalPresentationStyle = .overFullScreen
self.present(controller, animated: false, completion: nil)

Upvotes: 1

Mat
Mat

Reputation: 6324

Since you says "Those view controllers are created in the storyboard" I believe that you can use performSegueWithIdentifier and prepareForSegue:

Declare your variable outside your okButton method.

    let targetValue = 10 // this is just to test the first case
    let currentValue = 10 // this is just to test the first case
    var score = 0
    var point = 0

@IBAction func okButton(sender: AnyObject) {

        let diferencia = abs(targetValue - currentValue)
        point = 100 - diferencia
        score += point
        func bonusPoints(){
            score += 100

        }
        if diferencia == 0{
            performSegueWithIdentifier("GonPerfectViewController", sender: nil)
            point += 100
            print(point)
            newRound()
            updateLabel()
            bonusPoints()
        }else if diferencia < 4{

            performSegueWithIdentifier("GonNiceTry1ViewController", sender: nil)

            point += 50
            newRound()
            updateLabel()

        }else{

            performSegueWithIdentifier("GonThirdViewController", sender: nil)


            startnewgame()
            updateLabel()

        }

}

then you have to use prepareForSegue:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "GonPerfectViewController"{

        let vc = segue.destinationViewController as! GonPerfectViewController

        vc.point = point
    }
    if segue.identifier == "GonNiceTry1ViewController"{

        let vc = segue.destinationViewController as! GonNiceTry1ViewController

        vc.point = point
    }
    if segue.identifier == "GonThirdViewController"{

        let vc = segue.destinationViewController as! GonThirdViewController

        vc.point = point
    }


}

make sure you set segue identifier in every viewControllers

enter image description here

and of course you have to declare the var point also in your view controllers:

class GonPerfectViewController: UIViewController {

var point = 0

override func viewWillAppear(animated: Bool) {
    print(point)

}

}

you can find more info about segue here

Upvotes: 4

LinusG.
LinusG.

Reputation: 28952

So this should be the equivalent in Swift of Nirav Gadhiya's answer:

let storyboardName = "MainStoryboard_iPhone"
let viewControllerID = "ViewID"
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller = ViewController().storyboard?.instantiateViewControllerWithIdentifier(viewControllerID)
controller.infoDict = myDictToPass
self.presentViewController(controller!, animated: true, completion: nil)

Hope that helps :)

Upvotes: 1

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

No, you cant create segue programmatically.

Regarding information passing, just instatiate next view controller and pass information. Push or present with programming.

NSString * storyboardName = @"MainStoryboard_iPhone";
NSString * viewControllerID = @"ViewID";
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
MyViewController * controller = (MyViewController *)[storyboard instantiateViewControllerWithIdentifier:viewControllerID];
controller.infoDict = myDictToPass;
[self presentViewController:controller animated:YES completion:nil];

Just you need to find same code for swift... I hope you will do it...

Upvotes: 1

Related Questions