Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS6 UIPickerView内存泄漏问题。_Iphone_Objective C_Memory Leaks_Xcode4.5_Uipickerview - Fatal编程技术网

Iphone iOS6 UIPickerView内存泄漏问题。

Iphone iOS6 UIPickerView内存泄漏问题。,iphone,objective-c,memory-leaks,xcode4.5,uipickerview,Iphone,Objective C,Memory Leaks,Xcode4.5,Uipickerview,我得到了这个内存泄漏: [UIPickerTableViewTitleCell initWithStyle:resuableIdentifier]; 及 问题是我没有实现这个委托。在实现这一点之后,内存泄漏就消失了。也许这些信息对其他人有用,因为我花了16个小时才弄明白这个问题 // Do something with the selected row. - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteg

我得到了这个内存泄漏:

[UIPickerTableViewTitleCell initWithStyle:resuableIdentifier]; 

问题是我没有实现这个委托。在实现这一点之后,内存泄漏就消失了。也许这些信息对其他人有用,因为我花了16个小时才弄明白这个问题

// Do something with the selected row.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

// Get the text of the row.
NSString *rowItem = [NSString stringWithFormat:@"     %@",[machineData objectAtIndex:row]];

// Create and init a new UILabel.
// We must set our label's width equal to our picker's width.
// We'll give the default height in each row.
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];

// Make the text color red.
[lblRow setTextColor: [UIColor blackColor]];
[lblRow setFont:[UIFont boldSystemFontOfSize:20]];

// Center the text.
[lblRow setTextAlignment:UITextAlignmentLeft];

// Add the text.
[lblRow setText:rowItem];

// Clear the background color to avoid problems with the display.
[lblRow setBackgroundColor:[UIColor clearColor]];

// Return the label.
return lblRow;
}

谢谢你的信息。他被这次泄密弄糊涂了。只有几条评论:

  • 可能lblRow应该自动删除:
    return[lblRow autorelease]

  • [pickerView rowSizeForComponent:component]
    可用于获取新标签的大小


我在popover中使用了IUIPicker,每次我删除popover时都会出现内存泄漏。我也在使用ARC,所以解决这个问题的最简单方法是在卸载时设置UIPickerView=nil。下面的例子似乎已经成功了

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.pickerView = nil;
}

我使用的是ARC,所以这就是为什么它让生活变得有点简单。其次,我对ARC的了解是,它首先释放那些引用为零的对象。例如,像lblRow=nil;但我不知道“退货”的最佳做法是什么。我不知道为什么,但这是真的。我花了几个小时寻找那个内存泄漏。找到这篇文章后,我将pickerView:titleForRow:forComponent:替换为pickerView:viewForRow:forComponent:reusingView:,内存泄漏消失了!谢谢
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.pickerView = nil;
}