带有自定义 XIB 的自定义 UIView:IBOutlets 不起作用
我正在使用最新的 SDK 和 XCode 4.2 开发 iOS 应用程序。
我想在自定义 UIView 中使用自定义 XIB。
我的自定义 UIView:
@interface CoordinateView : UIView {
/**
*/
ARGeoCoordinate *geoCoordinate;
/**
*/
IBOutlet UILabel* title;
/**
*/
IBOutlet UILabel* subTitle;
/**
*/
IBOutlet UIImageView* image;
}
实现:
@implementation CoordinateView
@synthesize geoCoordinate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code.
//
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CoordinateView" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[CoordinateView class]])
{
[self addSubview:currentObject];
break;
}
}
}
return self;
}
- (id)initForCoordinate:(ARGeoCoordinate *)coordinate
{
self.geoCoordinate = coordinate;
CGRect theFrame = CGRectMake(0, 0, BOX_WIDTH, BOX_HEIGHT);
self = [self initWithFrame:theFrame];
if (self)
{
title.text = geoCoordinate.title;
image.image = [UIImage imageNamed:[NSString stringWithFormat:@"03%d.jpg", geoCoordinate.id]];
}
return self;
}
要初始化 CooperativeView
,我使用 initWithCooperative
方法。
调试我发现 title
在 initWithCooperative
中为零:
title.text = geoCoordinate.title;
我使用 Interface Builder 来“链接”IBOutlet。
我做错了什么?
I'm developing an iOS app with latest SDK and XCode 4.2.
I want to use a custom XIB in a custom UIView.
My custom UIView:
@interface CoordinateView : UIView {
/**
*/
ARGeoCoordinate *geoCoordinate;
/**
*/
IBOutlet UILabel* title;
/**
*/
IBOutlet UILabel* subTitle;
/**
*/
IBOutlet UIImageView* image;
}
Implementation:
@implementation CoordinateView
@synthesize geoCoordinate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code.
//
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CoordinateView" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[CoordinateView class]])
{
[self addSubview:currentObject];
break;
}
}
}
return self;
}
- (id)initForCoordinate:(ARGeoCoordinate *)coordinate
{
self.geoCoordinate = coordinate;
CGRect theFrame = CGRectMake(0, 0, BOX_WIDTH, BOX_HEIGHT);
self = [self initWithFrame:theFrame];
if (self)
{
title.text = geoCoordinate.title;
image.image = [UIImage imageNamed:[NSString stringWithFormat:@"03%d.jpg", geoCoordinate.id]];
}
return self;
}
To initialize CoordinateView
I use initWithCoordinate
method.
Debugging I've found that title
is nil here inside initWithCoordinate
:
title.text = geoCoordinate.title;
I've used Interface Builder to 'link' IBOutlets.
What I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不久前我遇到了同样的问题。以下是我修复该问题的方法:
只是个人喜好(不一定与讨论的主题相关):我会称你的 UILabel 为“titleLabel”而不是“title”,因为 [objectInstance title] 看起来会向不知道“title”的人返回一个 NSString是一个 UILabel。只是个人喜好,仅此而已。
干杯
I ran into the same issue a while back. Here is what I did to fix it:
Just a personal preference (not necessarily related to the topic on discussion): I would call your UILabel 'titleLabel' instead of 'title' because [objectInstance title] looks like it would return an NSString to someone who does not know that 'title' is a UILabel. Just a personal preference, thats all.
Cheers
在
initWithFrame:
方法中,您应该将self
分配给currentObject
,而不是将其添加为子视图。此外,无需重写 initWithFrame: 并调用 super 方法,因为视图的布局由 xib 文件确定。
In your
initWithFrame:
method, you should assignself
tocurrentObject
instead of adding it as a subview.Also there's no need to override
initWithFrame:
and calling thesuper
method since the layout of your view is determined by your xib file.当视图完全加载时,将调用 viewDidLoad 方法。因此,您应该在那里初始化视图中的元素:
文档。
When the view is totally loaded, the
viewDidLoad
method is called. So, you should initialize the elements within the view there:More info in the docs.