我看过相关文章,但我不明白为什么我的ViewController中没有调用它。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare in Start got triggered")
if segue.destination is RegistrationViewController {
let newVC = sender as? RegistrationViewController
newVC?.testString = "Mission accomplished, passed this data"
}
}
我通过按下按钮来调用
performSegue(withIdentifier: "RegistrationFromStart", sender: self)
testString是RegistrationViewController中的一个属性 segue发生了,没问题,但是属性从不改变,prepare也不会触发打印命令。
我在RegistrationViewController的viewDidLoad中打印testString
print("RegistrationViewController testString \(testString)")
谢谢您的帮助。
答案 0 :(得分:0)
简单地
let newVC = sender as? RegistrationViewController
newVC?.testString = "Mission accomplished, passed this data"
无效
由于sender
无法转换为RegistrationViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let des = segue.destination as? RegistrationViewController {
des.testString = "Mission accomplished, passed this data"
}
}
您的segue连接源应该是VC本身,而不是来自任何动作,并且应位于IBAction
内,例如set
performSegue(withIdentifier: "RegistrationFromStart", sender:nil)
答案 1 :(得分:0)
谢谢。感谢您指出这一点。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("prepare in Start got triggered")
if segue.destination is RegistrationViewController {
let newVC = segue.destination as? RegistrationViewController
newVC?.testString = "Mission accomplished, passed this data"
}
}
也就是说,它从不触发print语句,但是它确实传递了数据,这就是重点。 您的意思是,我是从IBAction调用它的
@IBAction func startKiosk(_ sender: Any) {
clickSound?.play()
performSegue(withIdentifier: "RegistrationFromStart", sender: self)
}