添加一个简单的UIAlertView

111

有什么起步代码可以用来制作一个带有一个“OK”按钮的简单UIAlertView吗?


你想要等待执行某个操作,直到点击“确定”按钮吗? - sudo rm -rf
1
@sudo rm -rf :不,我只需要它显示“ Dee dee doo doo”或其他内容。不需要执行任何操作。 - Linuxmint
9个回答

232

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

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

如果您想在按钮被点击时执行某些操作,请实现此委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    // the user clicked OK
    if (buttonIndex == 0) {
        // do something here...
    }
}

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

@interface YourViewController : UIViewController <UIAlertViewDelegate> 

4
如果你有多个警报视图并需要确定是谁调用了委托,你可以使用标签。 - Pnar Sbi Wer

77

其他答案已经提供了关于iOS 7及以下版本的信息,但是UIAlertView在iOS 8中已被弃用。

在iOS 8+中,您应该使用UIAlertController替代UIAlertViewUIActionSheet。文档: UIAlertController类参考。另外还有一篇关于NSHipster的好文章。

要创建一个简单的警告框(alert view),可以按照以下步骤进行:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title"
                                                                         message:@"Message"
                                                                  preferredStyle:UIAlertControllerStyleAlert];
//We add buttons to the alert controller by creating UIAlertActions:
UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"Ok"
                                                   style:UIAlertActionStyleDefault
                                                 handler:nil]; //You can use a block here to handle a press on this button
[alertController addAction:actionOk];
[self presentViewController:alertController animated:YES completion:nil];

Swift 3 / 4 / 5:

let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
//We add buttons to the alert controller by creating UIAlertActions:
let actionOk = UIAlertAction(title: "OK",
    style: .default,
    handler: nil) //You can use a block here to handle a press on this button

alertController.addAction(actionOk)

self.present(alertController, animated: true, completion: nil)

请注意,由于这段代码是在iOS 8中添加的,因此它无法在iOS 7及更早版本上运行。所以,不幸的是,现在我们必须像这样使用版本检查:

NSString *alertTitle = @"Title";
NSString *alertMessage = @"Message";
NSString *alertOkButtonText = @"Ok";

if (@available(iOS 8, *)) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                        message:alertMessage
                                                       delegate:nil
                                              cancelButtonTitle:nil
                                              otherButtonTitles:alertOkButtonText, nil];
    [alertView show];
}
else {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle
                                                                             message:alertMessage
                                                                      preferredStyle:UIAlertControllerStyleAlert];
    //We add buttons to the alert controller by creating UIAlertActions:
    UIAlertAction *actionOk = [UIAlertAction actionWithTitle:alertOkButtonText
                                                       style:UIAlertActionStyleDefault
                                                     handler:nil]; //You can use a block here to handle a press on this button
    [alertController addAction:actionOk];
    [self presentViewController:alertController animated:YES completion:nil];
}

Swift 3 / 4 / 5:

let alertTitle = "Title"
let alertMessage = "Message"
let alertOkButtonText = "Ok"

if #available(iOS 8, *) {
    let alertController = UIAlertController(title: alertTitle, message: alertMessage, preferredStyle: .alert)
    //We add buttons to the alert controller by creating UIAlertActions:
    let actionOk = UIAlertAction(title: alertOkButtonText,
        style: .default,
        handler: nil) //You can use a block here to handle a press on this button

    alertController.addAction(actionOk)
    self.present(alertController, animated: true, completion: nil)
}
else {
    let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText)
    alertView.show()
}

更新:已针对Swift 5进行了更新。在Objective-C中,使用可用性检查替换过时的类存在检查。


1
不应该发布可能能够工作但实际上并没有的代码。不要使用MyOwnUtilsClass,而是编写检查iOS版本的代码。 - csharpwinphonexaml
1
@csharpwinphonexaml,我不同意。这将使代码变得不必要地复杂。当前版本说明了UIAlerView / UIAlertController的使用,而系统版本检查不是此问题的主题。在Swift中,有一个内置的一行方法来检查操作系统版本,所以我使用了它。Objective-C有几种方法,但没有一种优雅。 - FreeNickname
1
我之所以这么说,是因为我知道并非每个人都擅长理解每段代码,并知道如何将其替换为可用的代码。 - csharpwinphonexaml

10

UIAlertView在iOS 8上已被弃用,因此,在iOS 8及以上版本中创建警告时,建议使用UIAlertController:

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
UIAlertView *myAlert = [[UIAlertView alloc] 
                         initWithTitle:@"Title"
                         message:@"Message"
                         delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Ok",nil];
[myAlert show];

9
UIAlertView *alert = [[UIAlertView alloc]
 initWithTitle:@"Title" 
 message:@"Message" 
 delegate:nil //or self
 cancelButtonTitle:@"OK"
 otherButtonTitles:nil];

 [alert show];
 [alert autorelease];

9
作为对之前两个回答(用户“sudo rm -rf”和“Evan Mulawski”的回答)的补充,如果您不想在单击警报视图时执行任何操作,您只需分配、显示和释放它即可。您不必声明委托协议。

3
这里有一个完整的方法,只有一个按钮“确定”来关闭UIAlert:
- (void) myAlert: (NSString*)errorMessage
{
    UIAlertView *myAlert = [[UIAlertView alloc]
                          initWithTitle:errorMessage
                          message:@""
                          delegate:self
                          cancelButtonTitle:nil
                          otherButtonTitles:@"ok", nil];
    myAlert.cancelButtonIndex = -1;
    [myAlert setTag:1000];
    [myAlert show];
}

1

此页面展示了如何在使用Swift时添加一个UIAlertController。


0

使用数组数据的简单警告:

NSString *name = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"Name"];

NSString *msg = [[YourArray objectAtIndex:indexPath.row ]valueForKey:@"message"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:name
                                                message:msg
                                               delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
[alert show];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接