带有自定义 XIB 的自定义 UIView:IBOutlets 不起作用

发布于 2025-01-02 18:52:59 字数 1641 浏览 2 评论 0原文

我正在使用最新的 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 方法。

调试我发现 titleinitWithCooperative 中为零:

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 技术交流群。

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

发布评论

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

评论(3

剪不断理还乱 2025-01-09 18:52:59

不久前我遇到了同样的问题。以下是我修复该问题的方法:

  1. 在 xib 中,打开文档大纲(左侧窗格显示视图及其子视图等列表)。
  2. Control left 单击代表您的类的视图(在您的情况下为 CooperativeView)并将蓝线向下拖动到子视图(在您的情况下为 UILabel 称为标题),
  3. 然后 XCode 应该会弹出一个带有选项“标题”的小对话框(因为您的标签称为 title。)
  4. 单击 title 和 wallaa。
  5. 返回到您的调试并检查您的出口是否仍然为零。

只是个人喜好(不一定与讨论的主题相关):我会称你的 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:

  1. In the xib, open the document outline (the left pane that shows the list of views and their subviews etc).
  2. Control left Click on the view representing your class (in your case CoordinateView) and drag the blue line down to the subviews (in your case the UILabel called title)
  3. XCode should then bring up a small dialog box with the option "title" (because your label is called title.)
  4. Click on title and wallaa.
  5. Go back to your debugging and check if your outlets are still nil.

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

你又不是我 2025-01-09 18:52:59

initWithFrame: 方法中,您应该将 self 分配给 currentObject,而不是将其添加为子视图。

此外,无需重写 initWithFrame: 并调用 super 方法,因为视图的布局由 xib 文件确定。

In your initWithFrame: method, you should assign self to currentObject instead of adding it as a subview.

Also there's no need to override initWithFrame: and calling the super method since the layout of your view is determined by your xib file.

可是我不能没有你 2025-01-09 18:52:59

当视图完全加载时,将调用 viewDidLoad 方法。因此,您应该在那里初始化视图中的元素:

- (void)viewDidLoad {
    title.text = self.geoCoordinate.title;
}

文档

When the view is totally loaded, the viewDidLoad method is called. So, you should initialize the elements within the view there:

- (void)viewDidLoad {
    title.text = self.geoCoordinate.title;
}

More info in the docs.

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