Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
iOS ARC UIAlertView内存泄漏_Ios_Memory Leaks_Automatic Ref Counting_Uialertview_Instruments - Fatal编程技术网

iOS ARC UIAlertView内存泄漏

iOS ARC UIAlertView内存泄漏,ios,memory-leaks,automatic-ref-counting,uialertview,instruments,Ios,Memory Leaks,Automatic Ref Counting,Uialertview,Instruments,我有一个用textfield显示AlertView的简单方法。显示此内存泄漏的仪器。请解释一下 - (void)method { NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:@"key"]; if (value == nil) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"mes

我有一个用textfield显示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;
}
}
请查看仪器的屏幕截图:

您需要将alertView创建为:

static UIAlertView *alertView = nil;

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

为什么要使alertView=nil?显示后?我在某个地方读到,要释放一个对象,它需要在使用后设置为零。从这里开始,是的。同意,但当您显示警报视图时,表示它仍然在层次结构中,并且您立即将其设置为零。在ARC中,它将处理它。如果您想这样做,请在使用后再做如何将其从层次结构中删除并释放(解除锁定)它?什么时候?为什么UIAlertView应该是静态的?因为当您调用此方法时,alertview会创建新实例。您可以声明全局alertiview而不是静态的。我想知道为什么会出现泄漏?那么,如何移除它呢?让我们来。