iOS ARC UIAlertView泄漏内存

时间:2017-05-05 07:18:53

标签: ios memory-leaks automatic-ref-counting uialertview instruments

我有简单的方法显示带有文本字段的AlertView。显示内存泄漏的仪器。请解释一下。

- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:@"key"];
if (value == nil) {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    alertView.tag = 101;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField *txtGroup = [alertView textFieldAtIndex:0];
    [txtGroup becomeFirstResponder];

    [alertView show];
    alertView = nil;
}
}

请找到仪器的截图: enter image description here

1 个答案:

答案 0 :(得分:0)

您需要将alertView创建为:

static UIAlertView *alertView = nil;

if (!alertView){
   alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
}