Objective c UIAlertView按钮会导致子NSThread
我想让用户得到一个提示屏幕,它将有是和否选项。 问题是无法从子线程调用Objective c UIAlertView按钮会导致子NSThread,objective-c,uialertview,nsthread,Objective C,Uialertview,Nsthread,我想让用户得到一个提示屏幕,它将有是和否选项。 问题是无法从子线程调用UIAlertView,如果从子线程调用它,则会出现运行时错误(EXC\u BAD\u ACCESS)。我正在IOS6.1上使用NSThread 这是我正在使用的代码 -(void) construct { NSThread *initThread =[[NSThread alloc]initWithTarget:self selector:@selector(error) object:nil]; [init
UIAlertView
,如果从子线程调用它,则会出现运行时错误(EXC\u BAD\u ACCESS
)。我正在IOS6.1上使用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
最有可能发生的情况是,UIAlertView是一个局部变量,它在委托有机会运行之前就被销毁了。您需要将对它的引用保留足够长的时间,以便您的代理有机会运行并完成它的工作。那你是清白的。此概念适用于ARC和基于内存管理的手动代码
请参阅此相关问题:最有可能发生的情况是,您的UIAlertView是一个局部变量,并且在委托有机会运行之前它就被销毁了。您需要将对它的引用保留足够长的时间,以便您的代理有机会运行并完成它的工作。那你是清白的。此概念适用于ARC和基于内存管理的手动代码
请参阅此相关问题:我尝试的代码实际上对我来说运行良好 唯一的区别是
[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];
});
}
我试过的代码实际上对我很有效
唯一的区别是
[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];
});
}
这是坠机的地方吗?事实上,这段代码对我来说很好,它也会很好。或者请提供更多关于ios版本和所有的详细信息..这是崩溃发生的地方吗?事实上,这段代码对我来说很好,它也会很好。或者请提供有关ios版本和所有内容的更多详细信息..我正在使用NSThread not queue我也尝试了queue,结果没有更改。我正在使用NSThread not queue我也尝试了queue,结果没有更改。谢谢,这就是我现在所做的,使用了一个简单的布尔值,在警报函数结束时初始化为false并将其值更改为true,当它为false时,保持睡眠100毫秒,这就是我现在所做的,使用一个简单的布尔值,在警报函数结束时初始化为false并将其值更改为true,当它为false时,保持睡眠100毫秒