didSelectRowAtIndexPath不会更改我传递给上一个View的属性

时间:2015-06-23 16:09:06

标签: ios uitableview didselectrowatindexpath

我正在尝试将数据从tableview传递到上一个屏幕,即我的注册屏幕。具体来说,我发送的是学校的名称。

当我通过segue返回上一个屏幕时,我正在更改的标签(schoolTextLabel)正在显示" None Selected",当我希望它显示时学校名称。通过一些错误检查,我发现当我拥有它println(theSchoolName)时,它正确地选择了我想要显示的名称,它只是没有将值传递给另一个屏幕。

请帮忙!
我在互联网上无休止地寻找答案,不能为我的生活找到答案 谢谢!

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 个答案:

答案 0 :(得分:0)

我找到了答案。我需要找到所选的行索引,然后在namesArray中调用该数字。我不确定为什么我以前的代码不起作用,但这现在有效。

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