添加一个简单的UIAlertView

什么是我可以使用一个简单的UIAlertView有一个“确定”button的启动代码?

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

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

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

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

并确保您的委托符合UIAlertViewDelegate协议:

 @interface YourViewController : UIViewController <UIAlertViewDelegate> 

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

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

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

 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:

 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 ([UIAlertController class] == nil) { //[UIAlertController class] returns nil on iOS 7 and older. You can use whatever method you want to check that the system version is 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:

 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) } else { let alertView = UIAlertView(title: alertTitle, message: alertMessage, delegate: nil, cancelButtonTitle: nil, otherButtonTitles: alertOkButtonText) alertView.show() } 

UPD:更新了Swift 3。

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

作为前两个答案(用户“sudo rm -rf”和“Evan Mulawski”)的补充,如果你不想在点击alert view的时候做任何事情,你可以分配,显示和释放它。 您不必声明委托协议。

在iOS 8上不build议使用UIAlertView。因此,要在iOS 8及更高版本上创build警报,build议使用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]; 

这是我实现它的方式。

 UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok",nil]; [myAlert show]; 

这是一个完整的方法,只有一个button,一个'好',closuresUIAlert:

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

如果您使用Swift, 此页面显示如何添加UIAlertController。

数组数据简单警报:

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

对于Swift 3:

 let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) self.present(alert, animated: true, completion: nil)