-1

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   
}
}
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
Anthony Saltarelli
  • 345
  • 1
  • 4
  • 15
  • Just because you've assigned a string to a label doesn't mean that any time you update the string, the label is updated. You need to update the label by setting it's text property, most likely in ViewWillAppear. Also, `prepareForSegue` is not called when navigating backwards in a `UINavigationController`. – Marcus Adams Jun 23 '15 at 20:00
  • I did not use a UINavigationController, it is just a View Controller with the table and a cancel button at the top. It is getting called, and I know this because "None Selected" is appearing instead of "No School Selected", so the label IS getting updated, however it is supposed to be sending whatever I select in the table. Do you know a fix for this or need more information? – Anthony Saltarelli Jun 23 '15 at 20:21

1 Answers1

0

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]
Anthony Saltarelli
  • 345
  • 1
  • 4
  • 15