Question

I would like a user to get a prompt screen, it would have yes and no options. The problem is the UIAlertView cannot be called from child thread, if I call it from child thread I am getting a runtime error(EXC_BAD_ACCESS). I am using NSThread on IOS6.1

This is the code that i am using

-(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;
    }
}

This is the error that I am getting:

[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
Was it helpful?

Solution

Most likely what is happening is that your UIAlertView is a local variable and it's getting destroyed before the delegate has a chance to run. You need to keep a reference to it around long enough such that your delegate has a chance to run and do it's work. Then you are in the clear. This concept applies to both ARC and manual memory managed based code.

See this related question: UIAlertViewDelegate class "self" instance gets dealloc'd before button gets pressed

OTHER TIPS

The code I tried actually worked fine for me

The only difference is

[alertView show] => [alertView show]; you miss ;,a typo in question i guess

running the code in seperate thread

 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];
    });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top