0

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

Ben
  • 355
  • 2
  • 12

5 Answers5

3

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.

fresskoma
  • 25,481
  • 10
  • 85
  • 128
  • can you explain more what you mean by "the delegate handle what happens inside the AlertView" – Rasman May 29 '11 at 20:06
  • @Rasman: Take a look at http://mobile.tutsplus.com/tutorials/iphone/uialertview/ which explains how you can create a UIAlertViewDelegate in order to handle button clicks from a UIAlertView – fresskoma May 29 '11 at 20:10
1

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

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
1

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];
    });
});
benzado
  • 82,288
  • 22
  • 110
  • 138
0

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..

stackr
  • 2,742
  • 27
  • 44
0

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???

Eman yalpsid
  • 511
  • 1
  • 5
  • 8