简体   繁体   English

以编程方式使用xib设置IBOutlet和文件所有者会导致键值编码兼容错误

[英]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并将其自身作为文件所有者吗?

Thank you nebs 谢谢你的小伙伴

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.

相关问题 删除情节提要并使用Xib会生成错误“类不符合键的键值编码要求” - Deleting Storyboard and using Xib generates error “class is not key value coding-compliant for the key” 加载 XIB 这个类不是键值编码兼容的键 - Load XIB this class is not key value coding-compliant for the key 将项目从情节提要更改为Xib文件时,出现“此类不符合键的键值编码”错误 - “This class is not key value coding-compliant for the key” error when changing project from storyboard to xib files 使用xib在新项目中始终会收到“不符合键值编码”错误 - Always get an error “not key value coding-compliant” on new project with xib 不是关键值编码兼容 - Not Key Value Coding-Compliant 无法识别iOS Xib文件IBOutlet-(不符合键值编码的类) - iOS xib file IBOutlet not recognized - (class not key-value coding compliant) 在NSDictionary中设置值时,此类不兼容键值编码 - This class is not key value coding-compliant while setting a value in NSDictionary Xcode 8错误:此类与键敌人TITLELabel的键值编码不兼容 - Xcode 8 error: this class is not key value coding-compliant for the key enemyTitleLabel 出现错误“NSUbiquitousKeyValueStore”,此 class 与键的键值编码不兼容 - Get error `NSUbiquitousKeyValueStore` this class is not key value coding-compliant for the key Swift 这个 class 不符合密钥错误的密钥值编码 - Swift this class is not key value coding-compliant for the key Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM