Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用故事板演示课堂_Swift_Uiviewcontroller_Storyboard - Fatal编程技术网

Swift 如何使用故事板演示课堂

Swift 如何使用故事板演示课堂,swift,uiviewcontroller,storyboard,Swift,Uiviewcontroller,Storyboard,我以编程方式编写UI,现在我想从故事板添加UI,当我尝试这样做时,编译器说在展开可选值时意外地发现了nil 在故事板->视图控制器->自定义类中,我设置了我的类。我还设置了ID board1 为什么它设置了零值 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { table.deselectRow(at: indexPath, animated: true)

我以编程方式编写UI,现在我想从故事板添加UI,当我尝试这样做时,编译器说在展开可选值时意外地发现了nil

在故事板->视图控制器->自定义类中,我设置了我的类。我还设置了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")

您似乎正在使用故事板的某个未知实例:

根据您的评论,故事板似乎不是由您声明的。这意味着它可能没有引用Main.storyboard文件,甚至可能为零

您应该创建一个引用Main.storyboard文件的新UIStoryboard实例,如下所示:

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let registerViewController = storyboard.instantiateViewController(withIdentifier: "board1") as? RegisterViewController

在哪一行中,你在展开可选值时意外地发现了nil?你能显示你的故事板吗?具体来说,VC的身份检查器。@zaitsman此行显示registerViewController!,动画:true,completion:nilIt看起来您使用的故事板不是您的主.storyboard文件。尝试用UIStoryboardname:Main,bundle:nil替换故事板。啊!这就解释了。是展示VC的故事板。由于您的表VC不是由序列图像板显示的,因此序列图像板为零。
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let registerViewController = storyboard.instantiateViewController(withIdentifier: "board1") as? RegisterViewController