swiftfor-loopnullsegueprepare

Unable to send an int value between view controller and table view controller in swift


the class PreferencesViewController sends nil through prepare for segue regardless if IBActions are taken or not. It seems that something is wrong in my prepare for segue but I'm not sure. I'm not trying to assign to any label or anything I'm just trying to send the int value to another variable in the class Pick2_1. Thanks in adavance import UIKit

class PreferencesViewController: UIViewController {
    var preference = 0


@IBAction func nImportant(sender: UIButton) {
    preference = -1
    print(preference)
}

@IBAction func N(sender: UIButton) {
    preference = 0
    print(preference)

}

@IBAction func mImportant(sender: UIButton) {
    preference = 1
    print(preference)

}


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



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

    if segue.identifier == "preferenceSegue" {
        let preferenceVC = segue.destinationViewController as! Pick2_1
        preferenceVC.preferenceSent = preference
        print(preference)


    }
}
}

Solution

  • Ok segue is directly connected to the button. You have to set the segue between your 2 controllers set the identifier and in the @IBAction you should call performSegueWithIdentifier func