释放添加到另一个视图的视图
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了避免内存泄漏,您应该在将这些视图添加为子视图后释放它们!
In order to avoid memory leaks, you should release these views after adding them as a subview!
这取决于。您是否在使用自动引用计数的 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.