ind*_*eel 8 iphone xcode uiview nib
我有一个小的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中的布局完全相同.但是一旦我尝试改变headline.text这样的话:
panelView.headline.text = @"Test";
Run Code Online (Sandbox Code Playgroud)
它崩溃了这个错误: 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIView标题]:无法识别的选择器发送到实例0x5c22b00'
它可能是我忽略的一些小东西,但到目前为止它已经让我疯了几个小时.有谁有想法吗?
Jas*_*oco 20
您将自定义视图的类重新分配给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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8979 次 |
| 最近记录: |