小编ind*_*eel的帖子

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

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

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

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

CoverPanel.h

@interface CoverPanel : UIView {

    IBOutlet UILabel *headline;

}

@property (nonatomic, retain) IBOutlet UILabel *headline;

@end
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

CoverPanel.xib,我已经链接UILabel到标题出口.在我的viewcontroller中,这是我如何创建CoverPanel实例,这就是它崩溃的地方:

CoverPanel *panelView = [[CoverPanel alloc] initWithFrame:CGRectMake(0,0,300,100)];
Run Code Online (Sandbox Code Playgroud)

到现在为止还挺好.它显示与UIView.xib中的布局完全相同.但是一旦我尝试改变 …

iphone xcode uiview nib

8
推荐指数
1
解决办法
8979
查看次数

标签 统计

iphone ×1

nib ×1

uiview ×1

xcode ×1