我已经尝试了几种加载它的方法,并且我已经在我的vIEwcontroller上成功显示了它.问题是,一旦我尝试在Interface Builder中实际链接IBOutlet并访问它,我的应用程序崩溃了.
我创建了一个自定义的UIVIEw类,如下所示:
CoverPanel.h
@interface CoverPanel : UIVIEw { IBOutlet UILabel *headline;}@property (nonatomic,retain) IBOutlet UILabel *headline;@end
CoverPanel.m
@implementation CoverPanel@synthesize headline;- (ID)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code. // self = [[[NSBundle mainBundle] loadNibnamed:@"CoverPanel" owner:self options:nil] objectAtIndex:0]; } return self;}
在CoverPanel.xib中,我将UILabel链接到标题出口.
在我的vIEwcontroller中,这是我创建CoverPanel实例的方式,这就是它崩溃的地方:
CoverPanel *panelVIEw = [[CoverPanel alloc] initWithFrame:CGRectMake(0,300,100)];
到现在为止还挺好.它完全按照它在.xib中的布局显示UIVIEw.
但是一旦我尝试更改headline.text就像这样:
panelVIEw.headline.text = @"Test";
它崩溃了这个错误:
因未捕获的异常’NSinvalidargumentexception’而终止应用程序,原因:’ – [UIVIEw标题]:无法识别的选择器发送到实例0x5c22b00′
这可能是我忽略的一些小事,但到目前为止,它已经让我疯了几个小时.有人有什么主意吗?
解决方法 您将自定义视图的类重新分配给xib中的视图.你不需要这样做因为那时你得到的是来自xib的视图,你刚刚泄露了你的CoverPanel.所以,只需更换初始化程序:- (ID)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // No need to re-assign self here... owner:self is all you need to get // your outlet wired up... UIVIEw* xibVIEw = [[[NSBundle mainBundle] loadNibnamed:@"CoverPanel" owner:self options:nil] objectAtIndex:0]; // Now add the vIEw to ourselves... [xibVIEw setFrame:[self bounds]]; [self addSubvIEw:xibVIEw]; // we automatically retain this with -addSubvIEw: } return self;}总结
以上是内存溢出为你收集整理的iphone – 从xib加载UIView,在尝试访问IBOutlet时崩溃全部内容,希望文章能够帮你解决iphone – 从xib加载UIView,在尝试访问IBOutlet时崩溃所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)