如何呈现 Class 故事板
how present Class with storyboard
我以编程方式编写 UI,现在我想从情节提要中添加 ui,当我尝试这样做时,编译器说 "Unexpectedly found nil while unwrapping an Optional value"
在故事板中 -> ViewController -> 自定义 Class 我设置了我的 class。我还设置了 ID "board1"
为什么设置为零值???
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)
}
您可以这样做:
// Perform the segue to the viewController you want. Make sure you set the identifier the same as in the storyboard
let VC1 = (self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier"))!
self.present(VC1, animated: true, completion: nil)
也可以为此写一个扩展:
extension UIViewController {
func presentView(withIdentifier: String) {
if let newVC = self.storyboard?.instantiateViewController(withIdentifier: withIdentifier) {
self.present(newVC, animated: true, completion: nil)
}
}
}
然后这样称呼它
self.presentView(withIdentifier: "yourIdentifier")
您似乎在此处使用了一些未知的 storyboard
实例:
let registerViewController = storyboard?.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
根据您的评论,storyboard
似乎不是您声明的。这意味着它可能不是指您的 Main.storyboard
文件,甚至可能是 nil。
您应该创建一个引用您的 Main.storyboard
文件的新 UIStoryboard
实例,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let registerViewController = storyboard.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
我以编程方式编写 UI,现在我想从情节提要中添加 ui,当我尝试这样做时,编译器说 "Unexpectedly found nil while unwrapping an Optional value"
在故事板中 -> ViewController -> 自定义 Class 我设置了我的 class。我还设置了 ID "board1" 为什么设置为零值???
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)
}
您可以这样做:
// Perform the segue to the viewController you want. Make sure you set the identifier the same as in the storyboard
let VC1 = (self.storyboard?.instantiateViewController(withIdentifier: "yourIdentifier"))!
self.present(VC1, animated: true, completion: nil)
也可以为此写一个扩展:
extension UIViewController {
func presentView(withIdentifier: String) {
if let newVC = self.storyboard?.instantiateViewController(withIdentifier: withIdentifier) {
self.present(newVC, animated: true, completion: nil)
}
}
}
然后这样称呼它
self.presentView(withIdentifier: "yourIdentifier")
您似乎在此处使用了一些未知的 storyboard
实例:
let registerViewController = storyboard?.instantiateViewController(withIdentifier: "board1") as? RegisterViewController
根据您的评论,storyboard
似乎不是您声明的。这意味着它可能不是指您的 Main.storyboard
文件,甚至可能是 nil。
您应该创建一个引用您的 Main.storyboard
文件的新 UIStoryboard
实例,如下所示:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let registerViewController = storyboard.instantiateViewController(withIdentifier: "board1") as? RegisterViewController