didSelectRowAtIndexPath is not changing the property I am passing to a previous View

68 views Asked by At

I am trying to pass data from a tableview to a previous screen, which is my Sign Up screen. Specifically what I am sending is the name of a school.

What is happening is that when I return to the previous screen through the segue, the label I am changing (schoolTextLabel) is showing "None Selected", when I want it to display the school name. Through some error checking, I have found that when I have it println(theSchoolName), it is correctly choosing the name I want displayed, It is just not passing the value to the other screen.

Please help!
I have searched endlessly on the internet for the answer and cannot for the life of me find the answer.
Thank you!

TableViewController

var namesArray = [String]()
var locationsArray = [String]()

var theSchoolName = "None Selected"


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "selectedSchool" {
        var completeSignUpVC = segue.destinationViewController as! SignUpViewController
        completeSignUpVC.school = self.theSchoolName
    }
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.theSchoolName = self.namesArray[indexPath.row]
    self.performSegueWithIdentifier("selectedSchool", sender: self.namesArray[indexPath.row])

    println(theSchoolName)
}

SignUpViewController

class SignUpViewController: UIViewController, UITextFieldDelegate {

@IBOutlet var schoolTextLabel: UILabel!
var school = "No School Selected"

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib. 
    self.schoolTextLabel.text = self.school        
    usernameSignupText.delegate = self
    passwordSignupText.delegate = self   
}
}
1

There are 1 answers

0
Anthony Saltarelli On BEST ANSWER

I found the answer. I needed to find the selected row index, and then call that number in the namesArray. I'm not exactly sure why my previous code didn't work, but this now works.

var selectedRowIndex = self.schoolTable.indexPathForSelectedRow()
var completeSignUpVC = segue.destinationViewController as! SignUpViewController
completeSignUpVC.school = namesArray[selectedRowIndex!.row]