【问题标题】:How to override the prepareForSegue with multiple Segue's如何用多个 Segue 覆盖 prepareForSegue
【发布时间】:2020-10-27 19:38:51
【问题描述】:

所以我正在学习 Core Data,并且我设法创建了一个具有父关系的 segue,它允许我根据您在之前的 VC(A 类别视图控制器)中选择的类别来继续显示某些项目。现在我在此之后创建了一个单独的 VC,它根据您的位置显示天气,要访问它,您必须单击原始 VC(类别视图控制器)中的按钮才能访问 weatherViewController。但是,每次我尝试这样做并执行 segue 时,都会遇到以下错误:

这是我到目前为止的代码:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    let destinationVC = segue.destination as! NoteTableViewController
    let weatherDestinationVC = segue.destination as! WeatherViewController
    
    if let indexLocale = noteTableView.indexPathForSelectedRow {
        destinationVC.selectionCategory = noteArray[indexLocale.row]
        print(indexLocale.row)
    }   else {
        print("Error")
        weatherDestinationVC.delegate = self
        performSegue(withIdentifier: "goToWeather", sender: self)
    }
    
}


@IBAction func goToWeatherView(_ sender: UIBarButtonItem) {
    performSegue(withIdentifier: "goToWeather", sender: self)
}

由于我只是在学习核心数据,因此我们将不胜感激!

【问题讨论】:

    标签: ios swift core-data uiviewcontroller segue


    【解决方案1】:

    有条件地转换不同的segue 目标视图控制器或检查segue 的identifier,无论您喜欢哪个。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let destinationVC = segue.destination as? NoteTableViewController {
            if let indexLocale = noteTableView.indexPathForSelectedRow {
                destinationVC.selectionCategory = noteArray[indexLocale.row]
                print(indexLocale.row)
            }
        } else if let weatherDestinationVC = segue.destination as? WeatherViewController {
            weatherDestinationVC.delegate = self
        }
    }
    

    另外,您不必在prepareForSegue 中使用performSegue

    【讨论】:

    • 谢谢伙计!解决了问题!感谢帮助!
    猜你喜欢
    • 2014-09-11
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    相关资源
    最近更新 更多