xcode 从xib加载UIView,尝试访问IBOutlet时崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6039209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading UIView from xib, crashes when trying to access IBOutlet
提问by indivisueel
I have a xib-file with a small UIView
on it, which in turn contains some labels. Now, I'm trying to load that UIView
into an existing UIViewController
, and change one of the label-texts. The reason I want to do it this way, is that the UIView
will be reused with different label-texts so I figured making a custom class and loading it from a xib would be the best way to go.
我有一个带有小号的 xib 文件UIView
,其中包含一些标签。现在,我正在尝试将其加载UIView
到现有的 中UIViewController
,并更改其中一个标签文本。我想这样做的原因是,UIView
将与不同的标签文本一起使用,所以我认为制作一个自定义类并从 xib 加载它是最好的方法。
I have tried several ways of loading it and I've successfully displayed it on my viewcontroller. The problem is, once I try to actually link an IBOutlet
in Interface Builder, and access it, my app crashes.
我尝试了几种加载它的方法,并成功地将它显示在我的视图控制器上。问题是,一旦我尝试IBOutlet
在 Interface Builder 中实际链接一个并访问它,我的应用程序就会崩溃。
I have created a custom UIView
class, which looks like this:
我创建了一个自定义UIView
类,如下所示:
CoverPanel.h
封面面板.h
@interface CoverPanel : UIView {
IBOutlet UILabel *headline;
}
@property (nonatomic, retain) IBOutlet UILabel *headline;
@end
CoverPanel.m
封面面板.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;
}
In the CoverPanel.xib
, I have linked the UILabel
to the headline outlet.
In my viewcontroller, here's how I create the CoverPanel
instance, and this is where it crashes:
在 中CoverPanel.xib
,我已将 链接UILabel
到头条新闻。在我的视图控制器中,这是我创建CoverPanel
实例的方式,这就是它崩溃的地方:
CoverPanel *panelView = [[CoverPanel alloc] initWithFrame:CGRectMake(0,0,300,100)];
So far, so good. It displays the UIView
exactly as it's layed out in the .xib.
But as soon as I try to change the headline.text
like so:
到现在为止还挺好。它UIView
完全按照 .xib 中的布局显示。但是一旦我尝试headline.text
像这样改变:
panelView.headline.text = @"Test";
it crashes with this error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView headline]: unrecognized selector sent to instance 0x5c22b00'
它因以下错误而崩溃: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView 标题]:无法识别的选择器发送到实例 0x5c22b00”
It might be something tiny I'm overlooking, but it has been driving me insane for hours and hours so far. Does anyone have any idea?
这可能是我忽略的一些小东西,但到目前为止它已经让我疯狂了几个小时。有谁有想法吗?
回答by Jason Coco
You reassigned your custom view's class to the view that lived in your xib. You don't need to do that because then what you got back was the view from the xib and you've just leaked your CoverPanel. So, simply replace your initializer:
您将自定义视图的类重新分配给位于 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;
}