Luuk D. Jansen
Luuk D. Jansen

Reputation: 4496

Cannot get UIAlert to work

Does somebody see something wrong with this? I have an UIAltertView but get an EXC_BAD_ACCESS when I click any of the two buttons:

    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"First Sync"];
    [alert setMessage:@"The App is going to do its first synchronisation. This might take a few moment..."];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"OK"];
    [alert addButtonWithTitle:@"Cancel"];
    [alert show];
    [alert release];

and to catch the response:

#pragma mark UIAlertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != 0)
    {
            NSLog(@"TEST1");
        return;
    }   
NSLog(@"TEST2");
}

It must be something simple...

Upvotes: 0

Views: 340

Answers (2)

Luuk D. Jansen
Luuk D. Jansen

Reputation: 4496

As hinted above, the problem was that the UIAlert was generated in a thread. By using an NSCondition and firing the Alert into a background thread I got the worker thread to wait while the background thread waits for a response from the user. Once the response comes it signals the worker thread to continue (as the required data is stored in the database at that point).

Upvotes: 0

diederikh
diederikh

Reputation: 25271

You'll have to use the designated initializer:

initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:

Upvotes: 1

Related Questions