从xib加载UIView,在尝试访问IBOutlet时崩溃 [英] Loading UIView from xib, crashes when trying to access IBOutlet

查看:150
本文介绍了从xib加载UIView,在尝试访问IBOutlet时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xib文件,上面有一个小的 UIView ,而后者又包含一些标签。现在,我正在尝试将 UIView 加载到现有的 UIViewController 中,并更改其中一个标签文本。我想这样做的原因是 UIView 将重复使用不同的标签文本,所以我想制作一个自定义类并从xib加载它将是最好的方法。

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.

我已经尝试了几种加载它的方法,并且我已经在我的viewcontroller上成功显示了它。问题是,一旦我尝试在Interface Builder中实际链接 IBOutlet 并访问它,我的应用就会崩溃。

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.

我创建了一个自定义 UIView 类,如下所示:

I have created a custom UIView class, which looks like this:

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 实例,这就是它崩溃的地方:

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 *panelView = [[CoverPanel alloc] initWithFrame:CGRectMake(0,0,300,100)];

到目前为止,非常好。它显示 UIView ,与它在.xib中的布局完全相同。
但是,一旦我尝试更改 headline.text ,就像这样:

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:

panelView.headline.text = @"Test";

它因此错误而崩溃:
由于未捕获的异常而终止应用'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?

推荐答案

您将自定义视图的类重新分配给xib中的视图。你不需要这样做因为那时你得到的是来自xib的视图,你刚刚泄露了你的CoverPanel。所以,只需替换你的初始值设定项:

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:

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

这篇关于从xib加载UIView,在尝试访问IBOutlet时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆