Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView runModal

Followup to Where is NSAlert.h in the iOS SDK?

Is there any way to get NSAlert runModal like behavior from a UIAlertView? Or from a UIActionSheet?

I'm planning on using only in debug builds so I'm not concerned with how it looks or if it uses undocumented functionality.

Edit:

NSAlert is part of the OS X SDK and is similar to MessageBox in Win32. It allows you to synchronously prompt the user for something. Here's an example:

NSAlert * myAlert=[[NSAlert alloc] init];
[myAlert setMessgeText:@"This is my alert"];
[myAlert addButtonWithTitle:@"button 1"];
[myAlert addButtonWithTitle:@"button 2"];

switch ([myAlert runModal]) {
  case NSAlertFirstButtonReturn:
    //handle first button
    break;
  case NSAlertSecondButtonReturn:
    //handle second button
    break;
}

runModal is a synchronous function, it shows the alert and waits for user response. Internally it is running a limited version of the message loop, but as far as the rest of my application is concerned, the world has stopped; no messages, no events, nothing.

like image 515
IronMensan Avatar asked Oct 18 '25 11:10

IronMensan


1 Answers

Internally it is running a limited version of the message loop, but as far as the rest of my application is concerned, the world has stopped

Just do exactly what you described: throw up the alert, then run the event loop till the alert view gets dismissed. This code works:

UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"O rlly?" message:nil delegate:nil
        cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
NSRunLoop *rl = [NSRunLoop currentRunLoop];
NSDate *d;
while ([alert isVisible]) {
    d = [[NSDate alloc] init];
    [rl runUntilDate:d];
    [d release];
}
[alert release];
like image 174
Jeremy W. Sherman Avatar answered Oct 21 '25 01:10

Jeremy W. Sherman