views:

39

answers:

0

I'm using UIAlertViews to display specific in-app messages and occasionally my app shows multiple alerts. The problem comes when I am attempting to access the bounds of the alerts.

Here is some code to illustrate the problem. This is put in a brand new View-based app:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self makeAlert:@"Zero alert" withMessage:@"This is the zero alert"];

    UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"First Alert" message:@"Here is the first alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [firstAlert show];
    NSLog(@"first alert bounds, origin: %f, %f  size: %f, %f",firstAlert.bounds.origin.x,firstAlert.bounds.origin.y,firstAlert.bounds.size.width,firstAlert.bounds.size.height);
    [firstAlert release];

    UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Second Alert" message:@"Here is the second alert" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [secondAlert show];
    NSLog(@"second alert bounds, origin: %f, %f  size: %f, %f",firstAlert.bounds.origin.x,firstAlert.bounds.origin.y,firstAlert.bounds.size.width,firstAlert.bounds.size.height);
    [secondAlert release];

    [self makeAlert:@"Third Alert" withMessage:@"Here is the third alert."];
    [self makeAlert:@"Fourth Alert" withMessage:@"Here is the fourth alert."];
}

- (void)makeAlert:(NSString *)makeTitle withMessage:(NSString *)makeMessage {
    UIAlertView *newAlert = [[UIAlertView alloc] initWithTitle:makeTitle message:makeMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [newAlert show];
    NSLog(@"%@ alert bounds, origin: %f, %f  size: %f, %f",makeTitle,newAlert.bounds.origin.x,newAlert.bounds.origin.y,newAlert.bounds.size.width,newAlert.bounds.size.height);
    [newAlert release];
}  

Add the makeAlert description to the .h file and run this app and you'll see the issue in the Log. The Zero Alert will show an origin of 0,0 and an appropriate width and height (284, 141 on 3/GS). All other alerts will show 0,0 for width, height.

If you comment out the Zero alert line (first [self makeAlert...]) then firstAlert and secondAlert will show the correct width and height, but third and fourth alerts will show 0,0.

No, my app doesn't show 4 or 5 alerts at a time. This is just an illustration. Creating an alert through a subroutine (or a loop) creates this bug.

I have a workaround right now that involves grabbing the width and height the first time I create the alert with an image and placing them in some class variables I've retained so I can use them later, but that's less than ideal (assumes all text and images will be same sized) and I also need to make sure I call an alert with an image first.

Any thoughts on why I'm getting 0,0 for width, height on these calls?