Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 仅在显示警报视图时,Exc_错误访问_Iphone_Objective C_Ios_Uialertview - Fatal编程技术网

Iphone 仅在显示警报视图时,Exc_错误访问

Iphone 仅在显示警报视图时,Exc_错误访问,iphone,objective-c,ios,uialertview,Iphone,Objective C,Ios,Uialertview,我有许多线程通过performLeachToRecainthread方法调用以下函数: -(void) showAlert: (NSString *)message{ if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) { NSLog(@"<< perform in main thread>>"); [self performSelectorOnMainThread:@selector(sh

我有许多线程通过performLeachToRecainthread方法调用以下函数:

-(void) showAlert: (NSString *)message{
if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
    NSLog(@"<< perform in main thread>>");
    [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(void)showAlert:(NSString*)消息{
如果([nsrunlop currentRunLoop]!=[nsrunlop mainRunLoop]){
NSLog(@“>”);
[self-performSelectorOnMainThread:@selector(showAlert:)with object:message waitUntilDone:NO];
}
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“信息”消息:消息委托:nil CancelButtontile:@“确定”其他Buttontiles:nil];
[警报显示];
}
因为,在任何情况下,此方法都将仅在主线程上调用,所以我不知道EXC_BAD_ACCESS崩溃的原因: [警报显示]


而这种崩溃只是偶尔发生的。请提供帮助。

问题在于如果条件为真或假,则alertview显示方法将起作用。 更改您的方法,如:

-(void) showAlert: (NSString *)message
{
 if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop])
 {
    NSLog(@"<< perform in main thread>>");
    [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
 }
 else
 {
   UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  [alert show];
 }
}
-(void)showAlert:(NSString*)消息
{
如果([nsrunlop currentRunLoop]!=[nsrunlop mainRunLoop])
{
NSLog(@“>”);
[self-performSelectorOnMainThread:@selector(showAlert:)with object:message waitUntilDone:NO];
}
其他的
{
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“信息”消息:消息委托:nil CancelButtontile:@“确定”其他Buttontiles:nil];
[警报显示];
}
}

我猜你忘了添加
返回在代码中,这样无论是否在主循环中,if下面的代码都会被执行

一个简单的解决方案可能是:

-(void) showAlert: (NSString *)message{
    if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
        NSLog(@"<< perform in main thread>>");
        [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
        return;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}
-(void)showAlert:(NSString*)消息{
如果([nsrunlop currentRunLoop]!=[nsrunlop mainRunLoop]){
NSLog(@“>”);
[self-performSelectorOnMainThread:@selector(showAlert:)with object:message waitUntilDone:NO];
返回;
}
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“信息”消息:消息委托:nil CancelButtontile:@“确定”其他Buttontiles:nil];
[警报显示];
}

在if块的末尾放置一个
返回值

-(void) showAlert: (NSString *)message{
if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
    NSLog(@"<< perform in main thread>>");
    [self performSelectorOnMainThread:@selector(showAlert:) withObject:message waitUntilDone:NO];
    return;
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Info" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(void)showAlert:(NSString*)消息{
如果([nsrunlop currentRunLoop]!=[nsrunlop mainRunLoop]){
NSLog(@“>”);
[self-performSelectorOnMainThread:@selector(showAlert:)with object:message waitUntilDone:NO];
返回;
}
UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@“信息”消息:消息委托:nil CancelButtontile:@“确定”其他Buttontiles:nil];
[警报显示];
}

备选方案为什么不使用此选项:

 [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
而不是

 [alert show];