这是我的代码:
UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError show];
当我点击确定按钮时,警报背景仍然存在,并且我点击屏幕时,状态栏闪烁。
我找到了一些答案,但是它们不起作用,我将代码更改为:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError show];
});
或设置代表:
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];
});
}
它仍然不起作用,然后我尝试:
UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"error" delegate:nil cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
不能工作,疯狂,这也不能工作:
[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];
}
仍然无法正常工作。
警报显示前后:
最佳答案
确保您的 View Controller (或您从中创建UIAlertView的任何位置)在@interface行中定义了“<UIAlertViewDelegate>
”。例如:
@interface YellowViewController : UIViewController <UIAlertViewDelegate>
现在,当您从子类化的 View Controller 创建UIAlertView并将委托(delegate)设置为self时,您的 View Controller 应符合并应接收委托(delegate)协议(protocol)方法。下件事这段代码对我来说是正确的:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *someError = [[UIAlertView alloc] initWithTitle:nil message:@"Server Error!" delegate:self cancelButtonTitle: @"ok" otherButtonTitles: nil];
[someError show];
});
但是您的委托(delegate)方法调用将在主线程上发生(这是所有UI发生的地方):-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[alertView dismissWithClickedButtonIndex:buttonIndex YES];
}
换句话说,摆脱那里的调度程序,看看会发生什么。再一次,the Apple documentation for " clickedButtonAtIndex:
" says:因此,您可能不需要实际显式关闭警报 View 。
关于iOS 6.1.2-为什么不关闭UIAlertView?仍然在屏幕上的背景,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16248558/