简体   繁体   English

字符串不通过segue?

[英]String not passing through segue?

I'm trying to build a cell that can be tapped, store the current text of the cell label, and send that label to a different view controller. 我正在尝试构建一个可以点击的单元格,存储单元格标签的当前文本,并将该标签发送到其他视图控制器。 I've looked everywhere and can't figure out why it's not passing 我到处都看了,不知道为什么没有过去

View controller with table view 带有表视图的视图控制器

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "openWave" {
        if let destVC = segue.destination as? GoToWaveViewController {
            destVC.waveLabel = sender as! String
            print("sdf: \(sender)")
        }
    }
} ...

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = self.tableView.cellForRow(at: indexPath) as! MyWavesTableViewCell
    let text = cell.waveLabel.text!
    let label = self.groupNames[indexPath.row]
    print(label)
    performSegue(withIdentifier: "openWave", sender: label)
    //self.tableView.deselectRow(at: indexPath, animated: true)}

Second View Controller 第二视图控制器

class GoToWaveViewController: UIViewController {

@IBOutlet weak var waveName: UILabel!
var waveLabel: String!

override func viewDidLoad() {
    super.viewDidLoad()

}

override func viewWillAppear(_ animated: Bool) {
    setProperties()
}

func setProperties() {
    self.waveLabel = self.waveName.text!
    print("asdf: \(waveLabel)")
    print("asdfa: \(self.waveLabel)")
}

I see it's a logical error you may need 我看这是您可能需要的逻辑错误

self.waveName.text =  self.waveLabel

Instead of 代替

self.waveLabel = self.waveName.text!

And that overwrites the sended value , so this 这会覆盖发送的值,所以这

print("asdf: \(waveLabel)")
print("asdfa: \(self.waveLabel)")

will print empty values 将打印空值

Well, when your view appears, you call setProperties , which overwrites self.waveLabel with the text from self.waveName . 好吧,当您的视图出现时,您将调用setProperties ,它将 self.waveLabel中的文本覆盖 self.waveName This line 这条线

self.waveLabel = self.waveName.text!

should be replaced with 应该替换为

self.waveName.text = self.waveLabel

Secondarily, fix your names! 其次,确定你的名字! Label is a bad name for something that is a string . Labelstring的坏名字。 If I were looking at this code, it's immediately think that waveName is a string and waveLabel is the label, but that is the opposite of what you have here. 如果我在看这段代码,会立即认为waveName是一个字符串, waveLabel是标签,但这与您在此使用的相反。

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

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