TableView和Button(崩溃)

时间:2016-09-21 18:39:03

标签: ios swift uitableview

我有一个包含这个prepareForSegue函数的表视图:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController
    let myindexpath = self.MainPageTableView.indexPathForSelectedRow
    let titleString = self.CategoryTitle.object(at: ((myindexpath as NSIndexPath?)?.row)!) as? String
    upcoming.titlestring = titleString
    self.MainPageTableView.deselectRow(at: myindexpath!, animated: true)
}

从6个单元格中获取标题并将其保存到nvigational栏标题中的下一个视图。

在包含segue功能准备的视图中,导航栏中有一个按钮,可以移动到另一个(第三个)视图控制器!

每当我点击该按钮时,应用程序就会在此行崩溃:

let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController

我知道错误的原因但我该如何解决?

1 个答案:

答案 0 :(得分:1)

根据你的segues和崩溃日志,我想你错过了这里。尝试使用您的segue标识符识别目的地:​​

func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ShowCategoryDevice" {
        let upcoming: CategoryDeviceViewController = segue.destination as! CategoryDeviceViewController
        let myindexpath = self.MainPageTableView.indexPathForSelectedRow
        let titleString = self.CategoryTitle.object(at: ((myindexpath as NSIndexPath?)?.row)!) as? String
        upcoming.titlestring = titleString
        self.MainPageTableView.deselectRow(at: myindexpath!, animated: true)

    }else  {
        let upcoming: PopOverViewController = segue.destination as! PopOverViewController
        //do rest of your stuff
    }
}