添加一个简单的UIAlertView

Lin*_*int 106 iphone cocoa-touch objective-c uialertview ios

我可以使用什么样的入门代码来制作一个简单的UIAlertView,上面有一个"OK"按钮?

sud*_*-rf 229

如果要显示警报,请执行以下操作:

    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];
Run Code Online (Sandbox Code Playgroud)

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

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

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

@interface YourViewController : UIViewController <UIAlertViewDelegate> 
Run Code Online (Sandbox Code Playgroud)

  • 如果您有多个警报视图,则可以使用标记来确定谁调用了委托. (4认同)

Fre*_*ame 69

其他答案已经提供了iOS 7及更早版本的信息,但UIAlertView在iOS 8中已弃用.

在iOS 8+中你应该使用UIAlertController.它取代了UIAlertViewUIActionSheet.文档:UIAlertController类参考.关于NSHipster的一篇很好的文章.

要创建简单的警报视图,您可以执行以下操作:

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];
Run Code Online (Sandbox Code Playgroud)

斯威夫特3/4:

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)
Run Code Online (Sandbox Code Playgroud)

请注意,由于它是在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];
}
Run Code Online (Sandbox Code Playgroud)

斯威夫特3/4:

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()
}
Run Code Online (Sandbox Code Playgroud)

UPD:更新为Swift 3/4.

UPD 2:从Xcode 9开始,在Objc-C中添加了@available的注释.


小智 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];
Run Code Online (Sandbox Code Playgroud)

这就是我实现它的方式.


Eva*_*ski 9

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

 [alert show];
 [alert autorelease];
Run Code Online (Sandbox Code Playgroud)


Di *_* Wu 9

作为前两个答案(用户"sudo rm -rf"和"Evan Mulawski")的补充,如果您在单击警报视图时不想执行任何操作,则可以分配,显示和释放它.您不必声明委托协议.


Bry*_*ira 9

UIAlertView *myAlert = [[UIAlertView alloc] 
                         initWithTitle:@"Title"
                         message:@"Message"
                         delegate:self
                         cancelButtonTitle:@"Cancel"
                         otherButtonTitles:@"Ok",nil];
[myAlert show];
Run Code Online (Sandbox Code Playgroud)