When I try to instantiate a UINib I get: this class is not key value coding-compliant for the key view

╄→гoц情女王★ 提交于 2020-01-16 07:50:32

问题


Followup on this question.

I have an app, where the main view has one button, and a file 'popup.xib' which is now empty (Just a view, no buttons, no labels, nothing). And I want it to popup when I press the button. For some reason I get the following error whenever I press the button:

... this class is not key value coding-compliant for the key view.'

I have read that this is because of outlets from the NIB which have been deleted or changed, that's why I removed all objects and what to display an empty view. But I still get this error.

My code:

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBAction func showPopup(sender: AnyObject) {
        var x = UINib(nibName: "Popup", bundle: nil);
        println(x)
        var y = x.instantiateWithOwner(nil, options: nil)

        println(y)

        var z = y[0] as? PopupViewController

        println(z)
        z!.show(self.view)
    }


}

PopupViewController.swift

import UIKit


class PopupViewController : UIViewController {

    func show(tView : UIView) {
        tView.addSubview(self.view)
        self.view.backgroundColor = UIColor.redColor()
    }

}

The output:

<UINib: 0x7ff822412f90>
2015-02-02 15:47:57.870 tttt[5437:179808] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x7ff822553ec0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
*** First throw call stack:

Update


Update 2

I continued tweaking stuff according to the answers and comments, and got if I do var y = x.instantiateWithOwner(PopupViewController(), options: nil) the line get executed OK. But then I get back an array containing a UIView, not a UIViewController. Therefore the last line z!.show(self.view) causes a crash. I know it creates the right nib, cause the properties (e.g. alpha value) that I changed appear in the text output from println(y) correctly.

So my question now is: What should I pass as the owner into instantiateWithOwner?


回答1:


Check what kind of class you have as the file owner. The error seems to be saying that it's trying to link the view in your .xib to a plain NSObject which, of course, has no view property.

If you don't want an owner that links to the view, make sure that there are no view outlets. Also verify that the file's owner is a type of object that really exists.




回答2:


You have to connect the File's Owners view property to the view in the interface builder.




回答3:


Maybe there are some messing in connections between StoryBoard XIB and swift classes (sorry I'm not with my iMac and I can't say you to send me a zip of the project) I'll write here steps I usually follow to accomplish such things, hoping this can help:

  • Create Xib file in your project. Place items on it as always. (ex: MyViewController.xib)
  • Create a new Swift class (name usually is the same as the Xib) (ex: MyViewController.swift)
  • In Interface Builder, select the xib, select the UIViewController and choose View > Utilities > Show Identity Inspector.
  • In the "Class" field select your custom swift class (MyViewController.swift)
  • Connect IBOutlets and IBActions if you need to
  • In MyViewController.swift add this extension:

    extension UIViewController {
    class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> UIViewController? {
        return UINib(
            nibName: nibNamed,
            bundle: bundle
            ).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
        }
    }
    
  • In your MainController add this line wherever you need it (in button click or other places)

    var x = MyViewController.loadFromNibNamed("MyViewController") as MyViewController
    

X now is instance of your MyViewController and Xib files is already loaded.



来源:https://stackoverflow.com/questions/28279401/when-i-try-to-instantiate-a-uinib-i-get-this-class-is-not-key-value-coding-comp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!