OGeek|极客世界-中国程序员成长平台

标题: ios - 通过按钮替换 ViewController,线程 1 : EXC_BAD_INSTRUCTION [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:26
标题: ios - 通过按钮替换 ViewController,线程 1 : EXC_BAD_INSTRUCTION

我想用一个 ViewController 替换我的 UITableViewController,里面有一个 UITableView。为此,我使用以下代码:

@objc func buttonAction(sender: UIButton!) {
    let newController = storyboard?.instantiateViewController(withIdentifier: "VcId") as! JobTableViewController
    self.navigationController?.present(newController, animated: true, completion: nil)
}

但我总是在第二行得到这个错误

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)



Best Answer-推荐答案


你应该这样做:

guard let newController = storyboard?.instantiateViewController(withIdentifier: "VcId") as? JobTableViewController else {return}
self.navigationController?.present(newController, animated: true, completion: nil)}

这个错误可能是因为在storyboard中没有找到viewcontroller,你可能给了错误的ID。

关于ios - 通过按钮替换 ViewController,线程 1 : EXC_BAD_INSTRUCTION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51736448/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4