没有任何按钮的UIAlertView

Bal*_*ram 3 iphone objective-c uialertview

我想知道以下代码是否正确。我试图从“ timedAlert”方法中删除2秒钟后(并且没有在alertView中的任何按钮)自动关闭alertView。

    //this is in another method  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [alert release];
    [self timedAlert];
}

-(void)timedAlert
{
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:2];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

如果将alertView的cancelButton设置为“ nil”,那么“ [alertView dismissWithClickedButtonIndex:0动画:是];”将如何处理?事情工作???我试着将cancelButton设为“ nil”,并且可以正常工作,但无法弄清楚如何...。

PS:我从另一个调用timedAlert方法

任何帮助表示赞赏!谢谢!

Nix*_*ack 5

首先,让我说,如果您使用自定义视图来处理此问题会更好,但是那样说问题似乎出在

[alert release];
Run Code Online (Sandbox Code Playgroud)

您要在完成处理之前释放对象(很奇怪,它不会崩溃)。

做这样的事情

// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
    [alertView release];
}
Run Code Online (Sandbox Code Playgroud)