ios - Storyboard?.instantiateViewController(withIdentifier : "") as! viewcontroller

标签 ios swift uitableview

我的自定义导航栏中有一个菜单表格 View 。如果我单击一个单元格,它应该将我带到另一个 View Controller 。但是当我点击它时我的应用程序崩溃了。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return menu_items.count  //It has 5 values
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")

    cell.textLabel?.text = menu_items[indexPath.row]
    cell.textLabel?.textColor = .white

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    let item = menu_items[indexPath.row]

    if (item == "Patienten") {


         tableView.removeFromSuperview()

        let vc =  self.storyboard?.instantiateViewController(withIdentifier: "patientView") as! patientViewVC //App crashes here
        self.present(vc, animated: false, completion: nil)

    } else if (item == "Mitarbeiter") {


    } else if (item == "Kalender") {


    } else if (item == "Tourenplanung") {


       tableView.removeFromSuperview()

        let vc =  storyboard?.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
        self.present(vc, animated: false, completion: nil)


    }else if (item == "Abmelden") {

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "loginvc") as! LoginVC  //App crashes here
        self.present(vc, animated: false, completion: nil)
    }


    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
    selectedCell.contentView.backgroundColor = colorLightGreen

}

}

我已经多次验证所有标识符名称和 View Controller 名称是否正确。

最佳答案

确保在实例化 viewController 之前加载正确的 Storyboard。

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let item = menu_items[indexPath.row]

    if (item == "Patienten") {
        tableView.removeFromSuperview()

        let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
        let vc =  storyBoard.instantiateViewController(withIdentifier: "patientView") as! patientViewVC
        self.present(vc, animated: false, completion: nil)

    } else if (item == "Mitarbeiter") {


    } else if (item == "Kalender") {


    } else if (item == "Tourenplanung") {


        tableView.removeFromSuperview()
        let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
        let vc =  storyBoard.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
        self.present(vc, animated: false, completion: nil)


    }else if (item == "Abmelden") {
        let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "loginvc") as! LoginVC  //App crashes here
        self.present(vc, animated: false, completion: nil)
    }


    let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
    selectedCell.contentView.backgroundColor = colorLightGreen

}

上面的StoryboardName是包含病人ViewVC、tourVC、LoginVC的 Storyboard的名称

关于ios - Storyboard?.instantiateViewController(withIdentifier : "") as! viewcontroller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46295943/

相关文章:

ios - 从字典数组中删除键值的重复项

ios - 注册 NSURLProtocol (URLProtocol) 类以与外部应用程序一起使用?

ios - 如何使用 CoreData 从选定的 Collection View 单元格中获取标签文本?

swift - 如何以编程方式打开 Safari Extension ToolbarItem 弹出窗口

ios - 未捕获的异常 'NSRangeException' ,原因 : '*** -[__NSArrayI objectAtIndex

swift - 单击 tvos 应用程序中 TableView 单元格内的 Collection View 单元格

ios - uilabel 上的罢工消失在 uitableview 中是否选择了单元格

ios - 更新到 Xcode 7.1(从 6.4)导致 UIIMageView 动画损坏

ios - 如何在应用程序启动后立即加载插页式广告

objective-c - 在 iOS 上加载 UIView 时如何运行一些代码?