gpt4 book ai didi

iOS 6.1.2 - 为什么不关闭 UIAlertView?背景仍然在屏幕上

转载 作者:可可西里 更新时间:2023-11-01 04:42:03 25 4
gpt4 key购买 nike

这是我的代码:

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];
}

还是不行。

警报显示前后:

before the alert show after tap ok

最佳答案

确保您的 View Controller (或您从中创建 UIAlertView 的任何地方)在@interface 行中定义了“<UIAlertViewDelegate>”。例如:

@interface YellowViewController : UIViewController <UIAlertViewDelegate>

现在,当您从您的子类 View Controller 创建一个 UIAlertView 并将委托(delegate)设置为自身时,您的 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];
}

换句话说,去掉里面的 dispatch 东西,看看会发生什么。然后,the Apple documentation for " clickedButtonAtIndex: " says :

Discussion

The receiver is automatically dismissed after this methodis invoked.

因此您可能不需要实际明确关闭警报 View 。

关于iOS 6.1.2 - 为什么不关闭 UIAlertView?背景仍然在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16248558/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com