来自不同xib的Swift init UIViewController崩溃

时间:2016-01-04 12:18:54

标签: swift uiviewcontroller

let xibs = ["UpdatePasswdViewController","FeedBackViewController",...]
let xibName = self.xibs[indexPath.row]
let pushVc: UIViewController = UIViewController.init(nibName: xibName, bundle: nil)
self.navigationController?.pushViewController(pushVc, animated: true)


class UpdatePasswdViewController: UIViewController {
    @IBOutlet var confirmNewPasswdTextField: UITextField!
}

' NSUnknownKeyException',原因:' [setValue:forUndefinedKey:]:此类与密钥confirmNewPasswdTextField不符合密钥值编码。' ***首先抛出调用堆栈:

当这样初始化时,它没问题。但是我在xibs数组中有很多viewController。

let pushVc: UpdatePasswdViewController = UpdatePasswdViewController.init(nibName: "UpdatePasswdViewController", bundle:nil)

1 个答案:

答案 0 :(得分:0)

我相信您的问题来自于使用UIViewControllerinit而不是创建您想要的子类的对象。这里有一些测试代码可以帮助您获得所需内容:

    let moduleName = "XibSwift"
    let xibs = ["TestViewController"]

    let controllerClass: AnyClass? = NSClassFromString("\(moduleName).\(xibs[0])")
    if let realControllerClass = controllerClass as! UIViewController.Type? {
        let vc = realControllerClass.init(nibName: xibs[0], bundle: nil)
        print(vc)
    }