开发者

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.


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];


You'll have to write your own if you want this behavior. Careful, if you block the main queue for too long, your app will be watchdog'd.

UIAlertView gives you modal behavior, and will end up working the same way your custom class will. You might consider using a block-based wrapper that wraps up UIAlertView and allows you to setup blocks for the button action callbacks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜