我可以使用什么样的入门代码制作一个简单的UIAlertView,上面有一个“OK”按钮?
答案 0 :(得分:227)
当您想要显示警报时,请执行以下操作:
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>
答案 1 :(得分:66)
其他答案已经提供了适用于iOS 7及更早版本的信息,但在iOS 8中已弃用 UIAlertView
。
在iOS 8+中,您应该使用UIAlertController
。它是UIAlertView
和UIActionSheet
的替代品。文档:UIAlertController Class Reference。关于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];
Swift 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)
请注意,由于它是在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+
// Starting with Xcode 9, you can also use
// 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:
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/4进行了更新。
UPD 2:从Xcode 9开始,在Objc-C中添加了@available的注释。
答案 2 :(得分: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];
这就是我实施它的方式。
答案 3 :(得分:9)
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:nil //or self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert autorelease];
答案 4 :(得分:9)
作为前两个答案(用户“sudo rm -rf”和“Evan Mulawski”)的补充,如果您在单击警报视图时不想执行任何操作,则可以分配,显示和释放它。您不必声明委托协议。
答案 5 :(得分:9)
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"Title"
message:@"Message"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Ok",nil];
[myAlert show];
答案 6 :(得分:3)
这是一个完整的方法,只有一个按钮,一个'ok',关闭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];
}
答案 7 :(得分:1)
This page显示了如果使用Swift,如何添加UIAlertController。
答案 8 :(得分: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];
答案 9 :(得分:-1)
对于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)