首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >添加一个简单的UIAlertView

添加一个简单的UIAlertView
EN

Stack Overflow用户
提问于 2010-12-17 01:51:24
回答 8查看 171.4K关注 0票数 111

我可以用什么开始代码来创建一个简单的UIAlertView,上面只有一个"OK“按钮?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2010-12-17 01:56:13

当您希望显示警报时,请执行以下操作:

代码语言:javascript
复制
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"ROFL" 
                                                    message:@"Dee dee doo doo." 
                                                    delegate:self 
                                                    cancelButtonTitle:@"OK" 
                                                    otherButtonTitles:nil];
[alert show];

    // If you're not using ARC, you will need to release the alert view.
    // [alert release];

如果要在单击按钮时执行某些操作,请实现此委托方法:

代码语言:javascript
复制
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // the user clicked OK
    if (buttonIndex == 0) {
        // do something here...
    }
}

并确保您的代理符合UIAlertViewDelegate协议:

代码语言:javascript
复制
@interface YourViewController : UIViewController <UIAlertViewDelegate> 
票数 232
EN

Stack Overflow用户

发布于 2015-11-21 06:49:57

iOS 8不推荐使用UIAlertView。因此,要在iOS 8及更高版本上创建警报,建议使用UIAlertController:

代码语言:javascript
复制
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"Alert Message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){

    // Enter code here
}];
[alert addAction:defaultAction];

// Present action where needed
[self presentViewController:alert animated:YES completion:nil];

这就是我实现它的方式。

票数 10
EN

Stack Overflow用户

发布于 2010-12-17 01:55:51

代码语言:javascript
复制
UIAlertView *alert = [[UIAlertView alloc]
 initWithTitle:@"Title" 
 message:@"Message" 
 delegate:nil //or self
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil];

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

https://stackoverflow.com/questions/4463806

复制
相关文章

相似问题

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