我正在定制一个UIPickerView的行,所以我在它的委托中实现了viewForRow方法,如下所示:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (view) {
return view;
} else {
NSString *s = [datePickerValues objectAtIndex:row];
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
l.text = s;
l.font = [UIFont boldSystemFontOfSize:18];
l.textAlignment = UITextAlignmentCenter;
l.backgroundColor = [UIColor purpleColor];
[l autorelease];
return l;
}
}我是新来Obj-C的。
因为我是aloc/initing l,所以我也应该根据内存管理指南来释放它。然而,我也需要返回它。自动释放可以吗?
发布于 2009-10-08 17:50:24
是的,自动释放就在这里。
发布于 2009-10-08 18:53:14
我认为惯例是在alloc语句中自动释放它:
UILabel *l = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 44)] autorelease];由于您需要对象在方法退出后仍然存在,因此您别无选择,只能使用自动释放。通常,您需要确保在调用方法中保留一个副本,否则它可能会随机释放给您。在这种情况下,pickerView会为您做这件事,所以不用担心。
https://stackoverflow.com/questions/1539442
复制相似问题