ios - 每次旋转组件时,UIPickerView 似乎都在泄漏

标签 ios cocoa-touch uipickerview

更新:

在我看来这几乎是一个 Apple 错误。我尝试了以下方法:

  1. 创建一个新的单窗口项目
  2. 创建一个如下所示的 UIPickerView,只是 选择器只允许转动刻度盘。也就是说,既不变量 与选择器交互时也不会操纵任何状态。

结果: 无论我是使用简单的 titleForRow:forComponent 还是 viewForRow:forComponent,每次转动拨盘时选择器仍然泄漏 48 个字节。此外,我什至尝试让选取器返回先前在数组中分配的 View ,并将其设置为 viewController 中的类属性,这样就不会保留或释放任何 View ,除非选取器在调用委托(delegate)方法之后或之前可能在内部执行任何操作。而且,仍然会发生泄漏。

对我来说似乎是一个 Apple bug。

原始问题

我有一个 textField,其 imputView 设置为具有 4 个组件的 UIPickerView。选择器用于选择成为文本字段中的文本的重量/质量。

在 Instruments 下,每次我转动 UIPickerView 中的转盘/组件时,都会发生泄漏:

enter image description here 这是 48 字节泄漏的堆栈跟踪:

enter image description here

UIPickerView获取各个组件 View 的代码是:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if(!view)
    {
        view = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 37)] autorelease];
        UILabel *label = (UILabel*)view;
        label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        label.textAlignment = UITextAlignmentCenter;
        label.font = [UIFont boldSystemFontOfSize:24];
        label.backgroundColor = [UIColor clearColor];
    }

    ((UILabel*)view).text = [NSString stringWithFormat:@"%@%i", ((component == 3) ? @"." : @""), row];
    return view;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    return  60;
}

将 textField 更新为新文本值的代码是:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *newWeight = [NSString stringWithFormat:@"%i%i%i.%i", 
                           [pickerView selectedRowInComponent:0], 
                           [pickerView selectedRowInComponent:1],
                           [pickerView selectedRowInComponent:2],
                           [pickerView selectedRowInComponent:3]];

    self.sampleMassBuffer = [NSDecimalNumber decimalNumberWithString:newWeight];
    weightTextField.text = [[numberFormatter stringFromNumber:self.sampleMassBuffer] stringByAppendingFormat:@" %@", currentConfiguration.weightUnits];
}

我不知道泄漏是如何产生的,或者它们是否是我的! 会不会是 Apple 的 bug?

最佳答案

我不确定这个问题是否还能回答,只是说您应该使用您的基本示例项目向 Apple 提交错误报告。

关于ios - 每次旋转组件时,UIPickerView 似乎都在泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9790243/

相关文章:

ios - 将按钮添加到 UIPickerView - Swift 1.2

ios - 无法更新 UILabel!在具有字符串值的 ViewController 中

ios - 在 NSArray 中改变 NSDictionary

ios - fatal error : Index out of range in swift while taking options from uipicker

ios - 如何使用 captureStillImageAsynchronouslyFromConnection 实现多重拍摄(iOS AVFoundation)

ios - 如何在两个 UIView 之间实现 "90% slide"

ios - 填充与在同一 View 的第一个选择器 View 中选择的值相对应的第二个选择器

ios - 为 restorationIdentifier 提供默认值

ios - 为什么我的按钮在 iOS 模拟器中会改变填充?

ios - 无法使用类型为 '[NSAttributedStringKey : Any]' 的索引下标类型为 'String' 的值