swift - 如何用故事板呈现课堂
问题描述
我以编程方式编写 UI,现在我想从情节提要中添加 ui,当我尝试这样做时,编译器说“Unexpectedly found nil while unwrapping an Optional value”
在 storyboard -> ViewController -> Custom Class 我设置了我的类。我还设置了 ID "board1" 为什么它设置 nil 值???
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
table.deselectRow(at: indexPath, animated: true)
let registerViewController = storyboard?.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
present(registerViewController!, animated: true, completion: nil)
}
解决方案
您似乎在storyboard
这里使用了一些未知的实例:
let registerViewController = storyboard?.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
根据您的评论,您storyboard
似乎没有声明。这意味着它可能不是指您的Main.storyboard
文件,甚至可能为零。
您应该创建一个UIStoryboard
引用您的Main.storyboard
文件的新实例,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let registerViewController = storyboard.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
推荐阅读
- oracle - 处理 ORA-01403: 未找到数据
- javascript - 使用纯 JavaScript (DOM) 和给定的对象数组创建表?
- java - p:rowExpansion 每次按下展开箭头时重复该组件
- perl - 通过准备和执行避免 SQL 注入
- python - 如何通过 group by 获得唯一 ID 的累积总和?
- php - Preg_grep() 正则表达式模式未按预期工作
- javascript - req.body 为空,获取请求
- python-3.x - 从包含多个附件的电子邮件中下载一个特定的 Outlook 附件
- ios - 以编程方式添加前导/顶部约束
- asp.net-core - 在 MVC 中将 Web 抓取逻辑放在哪里