Ios 虽然我在主线程上执行警报,但屏幕仍在闪烁
我知道UIKit应该在main线程上完成,这就是为什么,我确保我的警报视图显示在主线程上Ios 虽然我在主线程上执行警报,但屏幕仍在闪烁,ios,multithreading,uialertview,Ios,Multithreading,Uialertview,我知道UIKit应该在main线程上完成,这就是为什么,我确保我的警报视图显示在主线程上 -(void)showAlert:(NSString *)alertMessage{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertMessage message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; dispat
-(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];
});
}
但是,当我解除警报时,屏幕将闪烁。这并没有解决我的问题,我是不是遗漏了什么 如果从后台线程调用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];
为什么不将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]
});
}
在您的案例中,“确定”文本实际上应该是“取消”按钮。在您希望在msgDict中显示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];
}
您好,我在发布问题之前已经尝试过这种方法,但它根本没有帮助,在解除
警报后,仍然会出现到处闪烁的屏幕。好的,那么您确定这是由线程引起的吗?这也可能是由其他一些原因造成的。嗨,我的项目是ARC,不需要释放我的对象。嗨,Thanx对于这一点,你能解释一下为什么要使用NSMutableDictionary
?嗨,Malloc,我在这里使用NSMutableDictionary是为了将越来越多的值传递给方法showart:。如果您愿意在UIAlertView中发送多个OtherButtonTiles,那么它可能会更有用。此外,如果不使用字典,您将无法使用performSelectorOnMainThread发送多个参数: