我想把变量从第一个ViewController转移到第二个。
视图控制器1:
class First_VC: UIViewController {
...
@IBAction func touch(_ sender: UIButton) {
if let indexPath = table.indexPath(for: sender.superview!.superview as! UITableViewCell) {
let cell = table.cellForRow(at: indexPath)
selectedName = cell?.textLabel?.text
performSegue(withIdentifier: "segue", sender: self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.searchBar.delegate = self
self.table.dataSource = self
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard segue.identifier == "segue" else { return }
guard let vc2 = segue.destination as? Second_VC else { return }
vc2.name = selectedName
}
}
...
视图控制器2
class Second_VC: UIViewController {
...
var name: String?
override func viewDidLoad() {
super.viewDidLoad()
print(name ?? "1")
proName.text = name
}
}
当我在ViewController 2中使用打印时,我得到了这个:(两种不同的打印,延迟1秒)
"1"
"Yatoro雨" -> the variable I'm looking for (works only if performSegue is added)
在我的应用程序中,label. text根据第一次打印"1"更改,但看不到第二次打印。如何使xCode根据第二次打印更改proName. text?
1条答案
按热度按时间ivqmmu1c1#
下面的一些评论者建议通过视图控制器重新连接我的segue,并解释了原因。