Ben
Ben

Reputation: 355

UIAlertView not showing

I have a small problem with one of my UIAlertViews. I'm trying to show it, do some tasks, and then dismiss automatically. This is the code I'm using currently:

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[callingTaxi show];
/* Do some tasks */
[callingTaxi dismissWithClickedButtonIndex:0 animated:YES];
[callingTaxi release];

However, the UIAlertView only shows half-way. I can see the background darken, but then after the tasks have completed, the alert view quickly appears and then disappears, again.

Any ideas how to solve this issue?

Ben

Upvotes: 0

Views: 3313

Answers (5)

Eman yalpsid
Eman yalpsid

Reputation: 511

the uialertview is dismissed by: [callingTaxi dismissWithClickedButtonIndex:0 animated:YES]; before the user can read it.

How long do you intend to have the end user read it???

Upvotes: 0

benzado
benzado

Reputation: 84378

You don't need a delegate. The problem is that the tasks you do must happen on a background thread, so the main thread is left alone to update the screen.

If you update your code to use blocks and dispatch queues, everything will work:

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
[callingTaxi show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{
    /* Do some tasks */
    dispatch_async(dispatch_get_main_queue(), ^{
        // this code is back on the main thread, where it's safe to mess with the GUI
        [callingTaxi dismissWithClickedButtonIndex:0 animated:YES];
        [callingTaxi release];
    });
});

Upvotes: 1

fresskoma
fresskoma

Reputation: 25791

It does show, but you dismiss it right away, instead of waiting for the user to do something, with

[callingTaxi dismissWithClickedButtonIndex:0 animated:YES];

There is no time for iOS to render it completely. Anyway, this is not how dismissWithClickedButtonIndex is supposed to be used. What are you trying to achieve here?

Edit: I guess you need to assign a delegate to the UIAlertView, and let the delegate handle what happens inside the AlertView.

Upvotes: 3

stackr
stackr

Reputation: 2742

you should create a method for the alertview

- (void) alertView: (UIAlertView *) alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
// Do stuff
// Or if you have multiple buttons, you could use a switch

[callingTaxi release];

Or you could Autorelease..

But x3ro has given the correct answer, you call the method yourself instead of waiting for the user to press the button..

Upvotes: 0

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

You should not close it inside the same function which shows it, close it by timer, or as reaction to another event.

Upvotes: 1

Related Questions