如何在Swift中的视图控制器之间传递字典?

时间:2018-11-09 12:52:35

标签: ios swift

我正在尝试将此字典传递给另一个视图控制器

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "goToThird" {
        let Ps = segue.destination as! ThirdViewController
        Ps.hourDictionary = Time //time is a dictionary
    }
}

// Trigger send

@IBAction func buttonPressed(_ sender: Any) {
    performSegue(withIdentifier: "goToThird", sender: self)
}

这是目标视图控制器中的代码:

var hourDictionary : Dictionary = ["String" : "String"]

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell

    print(hourDictionary)
    cell.label2.text = hourDictionary["\(indexPath.row)"]

    return cell
}

但是hourDictionary仍保留以下值:

["String": "String"]
["String": "String"]
["String": "String"]
["String": "String"]
["String": "String"]
["String": "String"]

时间正在初始化为字典:

    var eatingTime : Dictionary = ["String" : "String"]

已经尝试替换func准备进行搜索,但仍无法正常工作:

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "goToThird" {
        let Ps = segue.destination as! ThirdViewController
        Ps.hourDictionary = Time
    }
}

0 个答案:

没有答案