iOS 6.1.2 - 为什么UIAlertView不被解雇?背景仍然在屏幕上

yel*_*low 5 objective-c uialertview ios ios6

这是我的代码:

UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError show];
Run Code Online (Sandbox Code Playgroud)

当我点击确定按钮时,警报背景仍然存在,我点击屏幕,状态栏闪烁.

我找到了一些答案,但它们不起作用,我将我的代码更改为:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});
Run Code Online (Sandbox Code Playgroud)

或设置代表:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:self cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:NO];
    });

}
Run Code Online (Sandbox Code Playgroud)

它仍然不起作用,然后我尝试:

UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"error" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)

不工作,疯了,这也不行:

[self performSelectorOnMainThread:@selector(showAlert:) withObject:@"error", nil)  waitUntilDone:NO];

// the show Alert function
-(void)showAlert:(NSString*)str
{
    UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message: str delegate: nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
    [someError performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}
Run Code Online (Sandbox Code Playgroud)

还是不行.

警报显示前后:

在警报节目之前 点击确定后

Mic*_*ann 3

确保您的视图控制器(或您从中创建 UIAlertView 的任何位置)<UIAlertViewDelegate>在 @interface 行中定义了“”。例如:

@interface YellowViewController : UIViewController <UIAlertViewDelegate>
Run Code Online (Sandbox Code Playgroud)

现在,当您从子类视图控制器创建 UIAlertView 并将委托设置为 self 时,您的视图控制器应该符合并应该接收委托协议方法。

接下来的事情。这段代码对我来说看起来是正确的:

dispatch_async(dispatch_get_main_queue(), ^{
       UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:self cancelButtonTitle: @"ok" otherButtonTitles: nil];
       [someError show];
});
Run Code Online (Sandbox Code Playgroud)

但是您的委托方法调用将在主线程上发生(这是所有 UI 发生的地方):

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView dismissWithClickedButtonIndex:buttonIndex YES];
}
Run Code Online (Sandbox Code Playgroud)

换句话说,去掉里面的调度东西,看看会发生什么。话又说回来,“”的苹果文档clickedButtonAtIndex:

讨论

调用此方法后,接收器将自动关闭。

因此,您可能不需要实际显式地关闭警报视图。