gpt4 book ai didi

ios - 当我尝试实例化 UINib 时,我得到 : this class is not key value coding-compliant for the key view

转载 作者:行者123 更新时间:2023-11-28 09:14:47 25 4
gpt4 key购买 nike

跟进this question .

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

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

我读到这是因为 NIB 的导出已被删除或更改,这就是为什么我删除了所有对象以及显示空 View 的内容。但我仍然收到此错误。

我的代码:

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()
}

}

输出:

<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:

更新

enter image description here


更新2

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

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

最佳答案

检查您作为文件所有者的类别。该错误似乎是说它试图将您的 .xib 中的 View 链接到一个普通的 NSObject,当然,它没有 View 属性。

如果您不想要链接到 View 的所有者,请确保没有 View socket 。还要验证文件的所有者是否是真正存在的对象类型。

关于ios - 当我尝试实例化 UINib 时,我得到 : this class is not key value coding-compliant for the key view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28279401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com