首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView runModal

UIAlertView runModal
EN

Stack Overflow用户
提问于 2011-07-08 14:24:54
回答 2查看 8.6K关注 0票数 3

Where is NSAlert.h in the iOS SDK?的后续行动

有没有办法从NSAlert runModal获得类似于UIAlertView的行为?还是来自UIActionSheet?

我计划只在调试版本中使用,所以我不关心它的外观,也不关心它是否使用无文档的功能。

编辑:

NSAlert是OS的一部分,与Win32中的MessageBox类似。它允许您同步提示用户某些内容。下面是一个例子:

代码语言:javascript
复制
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是一个同步函数,它显示警报并等待用户响应。在内部,它运行的是消息循环的有限版本,但就我的应用程序的其余部分而言,世界已经停止;没有消息,没有事件,什么也没有。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-08 17:33:37

内部,它正在运行消息循环的有限版本,但就我的应用程序的其余部分而言,世界已经停止了。

只需按照您所描述的那样做:抛出警报,然后运行事件循环,直到警报视图被排除。此代码适用于:

代码语言:javascript
复制
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];
票数 9
EN

Stack Overflow用户

发布于 2011-07-08 17:21:39

如果你想要这样的行为,你必须自己写。小心点,如果你把主队列阻塞太久,你的应用程序就会被监视。

UIAlertView给出了模式行为,并将以您的自定义类的方式结束工作。您可能会考虑使用一个基于块的包装器来封装UIAlertView,并允许您为按钮操作回调设置块。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6625872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档