从 xib 加载 UIView,尝试访问 IBOutlet 时崩溃

发布于 2024-11-07 13:40:00 字数 1532 浏览 0 评论 0原文

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

我尝试了多种加载它的方法,并成功地将它显示在我的视图控制器上。问题是,一旦我尝试在 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 链接到标题出口。 在我的视图控制器中,以下是我创建 CoverPanel 实例的方法,这就是它崩溃的地方:

CoverPanel *panelView = [[CoverPanel alloc] initWithFrame:CGRectMake(0,0,300,100)];

到目前为止,一切都很好。它完全按照 .xib 中的布局显示 UIView。 但是当我尝试像这样更改 headline.text 时:

panelView.headline.text = @"Test";

它会因以下错误而崩溃: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIView标题]:无法识别的选择器发送到实例0x5c22b00”

这可能是我忽略的一些小事情,但它一直让我发疯到目前为止已经好几个小时了。有人知道吗?

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.

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.

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

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)];

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";

it crashes with this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView headline]: unrecognized selector sent to instance 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ偶尔ゞ 2024-11-14 13:40:00

您将自定义视图的类重新分配给位于 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文