segue如何仅适用于某些指定的按钮

时间:2017-10-31 03:21:09

标签: swift xcode button segue pass-data

我正在使用Xcode做一个项目。我有四个按钮,其中两个是在视图之间传递数据,并希望其他两个按钮只是跳转到另一个视图而不传递任何数据。前两个表现非常好,将数据传递给另一个视图。但当我点击其余两个时,应用程序崩溃说:

  

" 2017-10-30 23:08:11.970500-0400 xxx [2557:65602]无法施放价值   类型' xxx.goalInfoViewController' (0x107f01580)来   ' xxx.IOViewController' (0x107f019a0)"

以下是代码:

@IBAction func monthlyIncomeAction(_ sender: Any) {
    numberForSegue = 1
    performSegue(withIdentifier: "outcomeSegue", sender: self)
}
@IBAction func MonthlyOutcomeAction(_ sender: Any) {
    numberForSegue = 2
    performSegue(withIdentifier: "incomeSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
    let ioController = segue.destination as! IOViewController
    ioController.segueIndex = numberForSegue
}

1 个答案:

答案 0 :(得分:0)

您应该确保只有两个在视图之间传递数据的segue才能在prepare方法中运行代码。让我们尝试下面的prepare方法代码。

override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
    if segue.identifier == "outcomeSegue" || segue.identifier == "incomeSegue" {
      return;
    }

    let ioController = segue.destination as! IOViewController
    ioController.segueIndex = numberForSegue
}  

或者

override func prepare(for segue: UIStoryboardSegue, sender: (Any)?) {
    if segue.destination is IOViewController {
        let ioController = segue.destination as! IOViewController
        ioController.segueIndex = numberForSegue
    }
}