简体   繁体   English

无法实例化视图控制器,Swift,iOS

[英]can not instantiate a view controller, swift, ios

I am trying to instantiate a view controller from the nib file like following : 我正在尝试从nib文件实例化视图控制器,如下所示:

class TNAChallengerHandler:ChallengeHandler {

    var controller : ViewController

    // Default initializer
    init(realm iRealm: String, controller iController : ViewController) {
        println("Default initializers")
        self.controller = iController;
        super.init(realm: iRealm)
    }

    // Convience initializer
    convenience init() {
        println("Convience initializers")
        let vc = ViewController(nibName: "ViewController", bundle: nil)

        self.init(realm: "SingleStepAuthRealm", controller:vc)
    }

Executing the codes and I am getting 执行代码,我越来越

iOS_SingleBasedAdapterAuthentication[40828:5708548] *** Terminating app due to uncaught
exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/tonytran/Library/Developer/CoreSimulator/Devices/6B797313-635F-4ABD-B3C6-A1D9DF3F1E85/
data/Containers/Bundle/Application/FDC3FD2B-6E26-4654-AC0F-ED3F44DDC5F6/iOS_SingleBasedAdapterAuthentication.app> (loaded)' with name 'ViewController''

I double check in the storyboard, the custom class of the class is already changed to ViewController like below and the name is as same as ViewController.swift 我在情节提要中仔细检查,该类的自定义类已经更改为如下所示的ViewController ,并且名称与ViewController.swift相同

在此处输入图片说明

Any thoughts about this. 关于此的任何想法。 All comments are welcomed here. 欢迎在这里发表所有评论。

You don't have a nib file named ViewController . 您没有一个名为ViewController的笔尖文件。

You have a storyboard. 你有一个故事板。 A storyboard is compiled into a set of nib files, but you don't access those nib files directly. 情节提要被编译为一组nib文件,但是您不能直接访问这些nib文件。 Instead, you need to give your view controller a Storyboard ID. 相反,您需要为视图控制器提供一个Storyboard ID。 Then you can ask the storyboard to instantiate that view controller. 然后,您可以要求情节提要实例化该视图控制器。

Let's say you use “GetData” as the Storyboard ID: 假设您使用“ GetData”作为情节提要ID:

故事板ID

and I assume your storyboard is named Main.storyboard . 我假设您的故事板名为Main.storyboard Then you can instantiate the view controller like this: 然后,您可以像这样实例化视图控制器:

convenience init() {
    println("Convenience initializers")
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("GetData")
    self.init(realm: "SingleStepAuthRealm", controller: vc)
}

That said, I'm not sure it makes a lot of sense to instantiate a view controller by name when that view controller is also used as the root view controller of a UINavigationController . 就是说,当该视图控制器还用作UINavigationController的根视图控制器时,我不确定通过名称实例化该视图控制器是否有意义。 Are you sure you want to instantiate a new view controller here? 您确定要在此处实例化新的视图控制器吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM