当我尝试实例化UINib时,我得到:这个类不是键视图的键值编码兼容 [英] When I try to instantiate a UINib I get: this class is not key value coding-compliant for the key view

查看:218
本文介绍了当我尝试实例化UINib时,我得到:这个类不是键视图的键值编码兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关注此问题

我有一个应用程序,主视图有一个按钮,文件'popup.xib'现在是空的(只是一个视图,没有按钮,没有标签,没有)。当我按下按钮时,我希望它弹出。出于某种原因,每当我按下按钮时都会出现以下错误:

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.'

我读过这是因为NIB的出口已被删除或更改,这就是我删除的原因所有对象以及显示空视图的内容。但我仍然会收到此错误。

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.

我的代码:

ViewController.swift

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

PopupViewController.swift

import UIKit


class PopupViewController : UIViewController {

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

}

输出:

<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

我根据答案和评论继续调整内容,如果我做了 var y = x.instantiateWithOwner(PopupViewController(),options:nil)行执行OK。但后来我得到一个包含UIView的数组,而不是UIViewController。因此,最后一行 z!.show(self.view)会导致崩溃。我知道它会创建正确的笔尖,导致我更改的属性(例如alpha值)出现在 println(y)的文本输出中。

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.

所以现在我的问题是:我应该将什么作为所有者传递到 instantiateWithOwner

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

推荐答案

也许在StoryBoard XIB和swift课程之间存在一些混乱(对不起我不是我的iMac,我不能说你给我发一个项目的拉链)
我会在这里写下我通常遵循的步骤来完成这些事情,希望这可以提供帮助:

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:


  • 创建Xib文件你的项目。一如既往地将物品放在上面。 (例如: MyViewController.xib

  • 创建一个新的Swift类(名称通常与Xib相同)(例如: MyViewController.swift

  • 在Interface Builder中,选择xib,选择UIViewController并选择View> Utilities> Show Identity Inspector。

  • 在类字段中选择自定义swift类( MyViewController.swift

  • Connect IBOutlets和IBActions如果您需要

  • MyViewController.swift 添加此扩展程序:

  • 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
    }
}


  • 在你的MainController中添加这一行你需要它(在按钮点击或其他地方)

  • In your MainController add this line wherever you need it (in button click or other places)

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


  • X现在是MyViewController的实例,Xib文件是已加载。

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

    这篇关于当我尝试实例化UINib时,我得到:这个类不是键视图的键值编码兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆