[英]Using xib programatically setting IBOutlet and file owner results in key value coding-compliant error
I create a xib. 我创建了一个xib。 The file owner of the xib is a set to a custom class called
PackageTrackingListViewElement
. xib的文件所有者是一个名为
PackageTrackingListViewElement
的自定义类的集合。 The PackageTrackingListViewElement
has an IBOutlet property called contentView
, among others. PackageTrackingListViewElement
具有一个名为contentView
的IBOutlet属性。 Several views in interface builder are connected to the IBOutlet properties of the custom class called PackageTrackingListViewElement
. 接口构建器中的多个视图已连接到名为
PackageTrackingListViewElement
的自定义类的IBOutlet属性。
I try to initialize an object from the xib. 我尝试从xib初始化一个对象。
PackageTrackingListViewElement *element2 = [[[NSBundle mainBundle] loadNibNamed:@"PackageTrackingListViewElementXib" owner:nil options:nil] objectAtIndex:0];
[element2.labelDate setText:@"12/14/15"];
[element2.labelLocation setText:@"Berkeley, California"];
After this, I add the element2
object to a UIStackView. 之后,我将
element2
对象添加到UIStackView中。
What am I supposed to set the file owner in loadNibNamed
so that I do not get the error that the owner of the xib is not key-value compliant for the properties I am attempting to access? 我应该如何在
loadNibNamed
设置文件所有者, loadNibNamed
出现以下错误:xib的所有者不符合我尝试访问的属性的键值要求?
My error is [<UIStackView 0x165ddc80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key contentView.'
我的错误是
[<UIStackView 0x165ddc80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key contentView.'
When I try to set the stackView as the owner of the xib. 当我尝试将stackView设置为xib的所有者时。 I know this is wrong of course, but I cannot set the object which has not been created to be its own owner.
我知道这当然是错误的,但是我无法将尚未创建的对象设置为其自己的所有者。 I do not understand what to do.
我不知道该怎么办。
Alas figured it out. las知道了。 Many thanks to Instantiating a UIView from a XIB file with itself as the file's owner?
非常感谢通过XIB文件实例化UIView并将其自身作为文件所有者吗?
PackageTrackingListViewElement.h PackageTrackingListViewElement.h
#import <UIKit/UIKit.h>
@interface PackageTrackingListViewElement : UIView
@property (nonatomic,strong) IBOutlet UILabel *labelLocation;
- (instancetype)initFromNib;
@end
PackageTrackingListViewElement.m PackageTrackingListViewElement.m
#import "PackageTrackingListViewElement.h"
@implementation PackageTrackingListViewElement : UIView
- (instancetype)initFromNib {
return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:nil options:nil] firstObject];
}
@end
You can then simply do: 然后,您可以简单地执行以下操作:
PackageTrackingListViewElement *myView = [[PackageTrackingListViewElement alloc] initFromNib];
[myView.labelLocation setText:@"text"]; //access properties
By using the instantiation procedure in the link above, I got my instantiation of the xib to work. 通过使用上面链接中的实例化过程,我使xib的实例化得以工作。 I set the file-owner of the xib to nothing (NSObject), and instead set the root view (by selecting the view in the xib) to the PackageTrackingListViewElement and connecting all outlets to that view.
我将xib的文件所有者设置为无(NSObject),而是将根视图(通过在xib中选择视图)设置为PackageTrackingListViewElement并将所有出口连接到该视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.