简体   繁体   English

Swift:在视图之间传递变量不会第一次更新

[英]Swift: Passing Variable between Views not updating first time

I have a tableview with cell data. 我有一个包含单元格数据的tableview。 When I click the cell, it takes me to another view controller and displays the cell label in a UIlabel on the new ViewController. 当我单击该单元格时,它将带我到另一个视图控制器,并在新ViewController的UIlabel中显示该单元格标签。

However, When I go back to the tableview and select a different cell, the value on the new view controller doesn't update immediately. 但是,当我返回到表格视图并选择其他单元格时,新视图控制器上的值不会立即更新。 It displays the last clicked cell, then if I go back and repeat the process a second time it will update. 它显示最后单击的单元格,然后如果我返回并再次重复该过程,它将更新。

didSelectRowAtIndexPath didSelectRowAtIndexPath方法

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

    self.labeltosend = currentCell.textLabel!.text!
}

prepareForSegue prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == PostSegueIdentifier {
        if let destination = segue.destinationViewController as? NewViewController {
            destination.newlabel = labeltosend
        }
    }
}

Am I supposed to reload the data somehow? 我是否应该以某种方式重新加载数据?

edit 1: Destination View Controller 编辑1:目标视图控制器

@IBOutlet weak var dispLabel: UILabel!
var newlabel = String()

override func viewWillAppear(animated: Bool) {
    dispLabel.text = newlabel
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.

}

prepareForSegue(_:sender) is not called if you draw segue from cell to next view controller. 如果从单元格到下一个视图控制器绘制segue ,则不会调用prepareForSegue(_:sender) Try to draw segue from view controller to view controller. 尝试从视图控制器到视图控制器绘制序列。 And perform a manual segue using performSegueWithIdentifier("MySegueIdentifier" sender:self) in tableView(_:didSelectRowAtIndexPath:indexPath) . 并使用tableView(_:didSelectRowAtIndexPath:indexPath) performSegueWithIdentifier("MySegueIdentifier" sender:self)执行手动tableView(_:didSelectRowAtIndexPath:indexPath) For more info please have a look at this answer . 有关更多信息,请查看此答案

Think prepareForSegue is sometimes getting called before didSelectRowAtIndexPath you can move that code to prepareForSegue. 认为有时在didSelectRowAtIndexPath之前会调用prepareForSegue,您可以将该代码移至prepareForSegue。 When you set up a segue as you have sender in prepareForSegue is the indexPath of row tap, precisely so you can do these sorts of things. 当您在prepareForSegue中将发送方设置为Segue时,正是分流的indexPath,因此您可以执行此类操作。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == PostSegueIdentifier {
        if let destination = segue.destinationViewController as? NewViewController {
            let indexPath = sender as NSIndexPath
            let currentCell = tableView.cellForRowAtIndexPath(indexPath)! as UITableViewCell

            self.labeltosend = currentCell.textLabel!.text!
            destination.newlabel = labeltosend
        }
    }
}

You should read Duncan's comment. 您应该阅读邓肯的评论。

First: if you have your segue connected from the UITableViewCell, don't. 第一:如果您的Segue是从UITableViewCell连接的,则不要。 Delete it and connect the segue from the view controller itself. 删除它,然后从视图控制器本身连接segue。

Second: on the didSelectRowAtIndexPath method, call prepareForSegue method. 第二:在didSelectRowAtIndexPath方法上,调用prepareForSegue方法。

And finally on the destination controller: 最后在目标控制器上:

@IBOutlet weak var dispLabel: UILabel!
var newlabel = String()

override func viewWillAppear(animated: Bool) {
    //dispLabel.text = newlabel
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do view setup here.
    dispLabel.text = newlabel

}

Also, you need to improve your naming convention. 另外,您需要改善命名约定。 Is really confusing 真是令人困惑

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM