0

我知道 UIKit 的东西应该在 mian 线程上完成,这就是为什么,我确保我的警报视图显示在主线程上。

-(void)showAlert:(NSString *)alertMessage{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    dispatch_async(dispatch_get_main_queue(), ^{
        [alert show];
    });

}

但是,当我解除警报时,屏幕会闪烁。所以这并没有解决我的问题,我错过了什么吗?

4

3 回答 3

0

如果您从后台线程调用 show alert 方法,请尝试使用

    [self performSelectorOnMainThread:@selector(showAlert:) withObject:alertMessage waitUntilDone:YES];

对于方法调用并将您的 showAlert 方法更改为

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
于 2013-04-18T12:26:35.003 回答
0

为什么不将 UIAlertView 分配到主线程中?此外,您的解决方案还有内存泄漏。尝试这个:

-(void)showAlert:(NSString *)alertMessage{


    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release]
    });

}

在您的情况下,“确定”文本实际上应该是取消按钮。

于 2013-04-18T12:45:21.093 回答
0

在您希望在 msgDict 中使用适当的 Title 和 Message 显示 UIAlertView 的位置编写以下代码。

    NSMutableDictionary *msgDict=[[NSMutableDictionary alloc] init];
    [msgDict setValue:@"Title for AlertView" forKey:@"Title"];
    [msgDict setValue:@"Message within the AlertView" forKey:@"Message"];

    [self performSelectorOnMainThread:@selector(showAlert:) withObject:msgDict  
                        waitUntilDone:YES];

然后程序控制到达 showAlert 方法内

    -(void)showAlert:(NSMutableDictionary *)msgDict
      {
           UIAlertView *alert=[[UIAlertView alloc] 
                       initWithTitle:[NSString stringWithFormat:@"%@",[msgDict objectForKey:@"Title"]] 
                             message:[NSString stringWithFormat:@"%@",[msgDict objectForKey:"Message"]]
                            delegate:nil 
                   cancelButtonTitle:nil 
                   otherButtonTitles:@"OK", nil];
           [alert show];
      }
于 2013-04-18T12:51:46.590 回答