【问题标题】:prepare(for segue:) not getting called准备(为 segue :) 没有被调用
【发布时间】:2018-09-21 19:31:12
【问题描述】:

我查看了相关帖子,但我不明白为什么我的 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)")

提前感谢您的帮助。

【问题讨论】:

  • 故事板中的segue连接是怎么做的?是来自 ViewController 还是来自按钮?

标签: ios swift uistoryboardsegue


【解决方案1】:

就是这样

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 中,例如设置

performSegue(withIdentifier: "RegistrationFromStart", sender:nil)

【讨论】:

  • 这一切都很好,但它没有解释为什么print("prepare in Start got triggered") 不打印,这就是问题所要问的。
  • 遵循正确的事情会使其对他有效,不一定是答案和问题的明确直接原因
  • 好的,我认为你是对的,他只是问的问题很糟糕。
【解决方案2】:

谢谢大家。感谢您指出这一点。

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)
 }

【讨论】:

    猜你喜欢
    • 2017-07-25
    • 2017-09-09
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2012-02-07
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多