2013-07-26 20 views
1

我想让用户获得提示屏幕,它会有和没有选项。 问题是UIAlertView不能从子线程调用,如果我从子线程调用它,我得到一个运行时错误(EXC_BAD_ACCESS)。我使用NSThread上IOS6.1UIAlertView按钮导致儿童NSThread

这是我使用

-(void) construct 
{ 
    NSThread *initThread =[[NSThread alloc]initWithTarget:self selector:@selector(error) object:nil]; 
    [initThread start]; 
} 

- (void) error 
{ 
    //make sure it runs on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Save" 
                  message:@"Enter File Name" 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"OK", nil]; 

     alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 

     [alertView show]; 
    }); 
} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    NSLog(@"Alert View dismissed with button at index %d",buttonIndex); 

    switch (alertView.alertViewStyle) 
    { 
     case UIAlertViewStylePlainTextInput: 
     { 
      UITextField *textField = [alertView textFieldAtIndex:0]; 
      NSLog(@"Plain text input: %@",textField.text); 
     } break; 
     case UIAlertViewStyleSecureTextInput: 
     { 
      UITextField *textField = [alertView textFieldAtIndex:0]; 
      NSLog(@"Secure text input: %@",textField.text); 
     } break; 
     case UIAlertViewStyleLoginAndPasswordInput: 
     { 
      UITextField *loginField = [alertView textFieldAtIndex:0]; 
      NSLog(@"Login input: %@",loginField.text); 

      UITextField *passwordField = [alertView textFieldAtIndex:1]; 
      NSLog(@"Password input: %@",passwordField.text); 
     }break; 
     default: break; 
    } 
} 

这个代码是我得到的错误:

[NSURLCacheInternal alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x8653630 
2013-07-26 16:12:35.738 iOSTrack[7455:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLCacheInternal alertView:didDismissWithButtonIndex:]: unrecognized selector sent to instance 0x8653630' 
*** First throw call stack: 
(0x1e7a012 0x1796e7e 0x1f054bd 0x1e69bbc 0x1e6994e 0x7c0e13 0x408d66 0x408f04 0x20e7d8 0x266b014 0x265b7d5 0x1e20af5 0x1e1ff44 0x1e1fe1b 0x1dd47e3 0x1dd4668 0x3caffc 0x21cd 0x20f5) 
libc++abi.dylib: terminate called throwing an exception 
+0

这是碰撞发生的地方吗?其实代码对我来说工作得很好 –

+0

它会正常工作。或者请提供更多关于ios版本和所有.. –

回答

1

看上去最有可能的情况是,你的UIAlertView是一个局部变量,它在代理有机会运行之前就被破坏了。您需要长时间保持参考,以便您的代表有机会运行并执行工作。然后你清楚了。这个概念适用于基于ARC和手动内存管理的代码。

请参阅此相关的问题:UIAlertViewDelegate class "self" instance gets dealloc'd before button gets pressed

+0

谢谢,这就是我现在所做的,使用了一个简单的布尔值,初始化为false,并在alert函数结束时将其值更改为true,而它是假的,保持睡眠100ms –

0

我尝试的代码实际上对我来说工作得很好

唯一的区别是

[alertView show] =>[alertView show];你错过;,问题一个错字我猜

在单独线程中运行代码

dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL); 
    dispatch_async(myQueue, ^{ 
     // Perform long running process 
     [self error]; 
    }); 

- (void) error { 
    //make sure it runs on the main thread 
    dispatch_async(dispatch_get_main_queue(), ^{ 

     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Save" 
                  message:@"Enter File Name" 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
                otherButtonTitles:@"OK", nil]; 

     alertView.alertViewStyle = UIAlertViewStylePlainTextInput; 

     [alertView show]; 
    }); 
} 
+0

我正在使用NSThread而不是队列 另外我试过队列,结果没有改变。 –