释放添加到另一个视图的视图

发布于 2024-12-08 08:48:32 字数 2445 浏览 0 评论 0原文

我正在开发一个 iPhone 应用程序,使用以下方法:

- (void)drawView:(ARCoordinate *)coordinate {

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [titleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [titleLabel setTextColor:       [UIColor whiteColor]];
    [titleLabel setTextAlignment:   UITextAlignmentCenter];
    [titleLabel setText:            [coordinate title]];
    [titleLabel sizeToFit];
    [titleLabel setFrame:   CGRectMake(BOX_WIDTH / 2.0 - [titleLabel bounds].size.width / 2.0 - 4.0, 
                                       0, 
                                       [titleLabel bounds].size.width + 8.0, 
                                       [titleLabel bounds].size.height + 8.0)];

    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [subTitleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [subTitleLabel setTextColor:        [UIColor whiteColor]];
    [subTitleLabel setTextAlignment:    UITextAlignmentCenter];
    [subTitleLabel setText:         [coordinate subtitle]];
    [subTitleLabel sizeToFit];
    [subTitleLabel setFrame: CGRectMake(BOX_WIDTH / 2.0 - [subTitleLabel bounds].size.width / 2.0 - 4.0, 
                                        21.0, 
                                        [subTitleLabel bounds].size.width + 8.0,
                                        [subTitleLabel bounds].size.height + 8.0)];

    UIImageView *pointView  = [[UIImageView alloc] initWithFrame:CGRectZero];
    [pointView setImage:    [UIImage imageNamed:@"location.png"]];
    [pointView setFrame:    CGRectMake((int)(BOX_WIDTH / 2.0 - [pointView image].size.width / 2.0),
                                       (int)(BOX_HEIGHT / 2.0 - [pointView image].size.height / 2.0),
                                       [pointView image].size.width, [pointView image].size.height)];

    [self addSubview:titleLabel];
    [self addSubview:subTitleLabel];
    [self addSubview:pointView];

    self.backgroundColor = [UIColor clearColor];
}

此代码运行良好,并且编译器不会抱怨它。但我不确定为什么我不必执行以下操作:

[titleLabel release];
[subTitleLabel release];
[pointView release];

我认为,这是因为这些视图被添加到另一个视图(此方法位于继承自 View 的类中) )。

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];

有什么建议吗?

I'm developing an iPhone application, with the following method:

- (void)drawView:(ARCoordinate *)coordinate {

    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [titleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [titleLabel setTextColor:       [UIColor whiteColor]];
    [titleLabel setTextAlignment:   UITextAlignmentCenter];
    [titleLabel setText:            [coordinate title]];
    [titleLabel sizeToFit];
    [titleLabel setFrame:   CGRectMake(BOX_WIDTH / 2.0 - [titleLabel bounds].size.width / 2.0 - 4.0, 
                                       0, 
                                       [titleLabel bounds].size.width + 8.0, 
                                       [titleLabel bounds].size.height + 8.0)];

    UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, BOX_WIDTH, 20.0)];

    [subTitleLabel setBackgroundColor: [UIColor colorWithWhite:.3 alpha:.8]];
    [subTitleLabel setTextColor:        [UIColor whiteColor]];
    [subTitleLabel setTextAlignment:    UITextAlignmentCenter];
    [subTitleLabel setText:         [coordinate subtitle]];
    [subTitleLabel sizeToFit];
    [subTitleLabel setFrame: CGRectMake(BOX_WIDTH / 2.0 - [subTitleLabel bounds].size.width / 2.0 - 4.0, 
                                        21.0, 
                                        [subTitleLabel bounds].size.width + 8.0,
                                        [subTitleLabel bounds].size.height + 8.0)];

    UIImageView *pointView  = [[UIImageView alloc] initWithFrame:CGRectZero];
    [pointView setImage:    [UIImage imageNamed:@"location.png"]];
    [pointView setFrame:    CGRectMake((int)(BOX_WIDTH / 2.0 - [pointView image].size.width / 2.0),
                                       (int)(BOX_HEIGHT / 2.0 - [pointView image].size.height / 2.0),
                                       [pointView image].size.width, [pointView image].size.height)];

    [self addSubview:titleLabel];
    [self addSubview:subTitleLabel];
    [self addSubview:pointView];

    self.backgroundColor = [UIColor clearColor];
}

This code works well, and compiler doesn't complain about it. But I'm not sure why I haven't had to do the following:

[titleLabel release];
[subTitleLabel release];
[pointView release];

I think, it is due to that these views are added to another view (this method is in a class that inherits from View).

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];

Any advice?

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

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

发布评论

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

评论(2

猛虎独行 2024-12-15 08:48:32

为了避免内存泄漏,您应该在将这些视图添加为子视图后释放它们!

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];    

[titleLabel release];
[subTitleLabel release];
[pointView release];

In order to avoid memory leaks, you should release these views after adding them as a subview!

[self addSubview:titleLabel];
[self addSubview:subTitleLabel];
[self addSubview:pointView];    

[titleLabel release];
[subTitleLabel release];
[pointView release];
花开雨落又逢春i 2024-12-15 08:48:32

这取决于。您是否在使用自动引用计数的 iOS 5 上执行此操作?那么你就不再需要调用release了。

如果你使用的是 iOS 4 或者没有使用 ARC,那么如果你不进行发布,编译器不会抱怨,但你的代码肯定会泄漏内存。

尝试通过 Xcode 分析器运行此代码,这应该会向您指出。

It depends. Are you doing this on iOS 5 with Automated Reference Counting? Then you no longer need to call release.

If you're on iOS 4 or not using ARC then the compiler won't complain if you don't do the releases, but your code certainly does leak memory.

Try running this code through the Xcode Analyzer, that should point it out to you.

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