我想创建一个警告类型框,以便当用户尝试删除某些内容时,它会说“你确定”,然后如果他们确定则为是或否。在iPhone上做这个的最好方法是什么?
答案 0 :(得分:60)
UIAlertView
是最好的方法。它将动画显示在屏幕中间,使背景变暗,并强制用户解决它,然后返回到应用程序的正常功能。
您可以像这样创建UIAlertView
:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this. This action cannot be undone" delegate:self cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
这将显示消息。
然后检查他们是否点击了删除或取消,请使用:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
//delete it
}
}
请确保在您的标题文件(.h
)中,通过UIAlertViewDelegate
添加<UIAlertViewDelegate>
,在您的类继承的任何内容旁边(即。UIViewController
或{ {1}}等。)
有关UITableViewController
的所有细节的详情,请查看Apple's Docs Here
希望有所帮助
答案 1 :(得分:14)
帖子很旧,但仍然是一个很好的问题。在iOS 8中,答案已经改变。今天你宁愿使用&#39; UIAlertController&#39;使用&#39; preferredStyle&#39; &#39; UIAlertControllerStyle.ActionSheet&#39;。
这样的代码(swift)绑定到一个按钮:
@IBAction func resetClicked(sender: AnyObject) {
let alert = UIAlertController(
title: "Reset GameCenter Achievements",
message: "Highscores and the Leaderboard are not affected.\nCannot be undone",
preferredStyle: UIAlertControllerStyle.ActionSheet)
alert.addAction(
UIAlertAction(
title: "Reset Achievements",
style: UIAlertActionStyle.Destructive,
handler: {
(action: UIAlertAction!) -> Void in
gameCenter.resetAchievements()
}
)
)
alert.addAction(
UIAlertAction(
title: "Show GameCenter",
style: UIAlertActionStyle.Default,
handler: {
(action: UIAlertAction!) -> Void in
self.gameCenterButtonClicked()
}
)
)
alert.addAction(
UIAlertAction(
title: "Cancel",
style: UIAlertActionStyle.Cancel,
handler: nil
)
)
if let popoverController = alert.popoverPresentationController {
popoverController.sourceView = sender as UIView
popoverController.sourceRect = sender.bounds
}
self.presentViewController(alert, animated: true, completion: nil)
}
会产生这个输出:
编辑:代码在iPad,iOS 8+上崩溃。如果按照此处所述添加必要的行:on another stack overflow answer
答案 2 :(得分:2)
对于swift来说,它非常简单。
//Creating the alert controller
//It takes the title and the alert message and prefferred style
let alertController = UIAlertController(title: "Hello Coders", message: "Visit www.simplifiedios.net to learn xcode", preferredStyle: .Alert)
//then we create a default action for the alert...
//It is actually a button and we have given the button text style and handler
//currently handler is nil as we are not specifying any handler
let defaultAction = UIAlertAction(title: "Close Alert", style: .Default, handler: nil)
//now we are adding the default action to our alertcontroller
alertController.addAction(defaultAction)
//and finally presenting our alert using this method
presentViewController(alertController, animated: true, completion: nil)
答案 3 :(得分:1)
每个人都在说UIAlertView。但要确认删除,UIActionSheet可能是更好的选择。见When to use a UIAlertView vs. UIActionSheet
答案 4 :(得分:0)
UIAlertView似乎是确认的明显选择。
将委托设置为控制器并实现UIAlertViewDelegate协议http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html
答案 5 :(得分:0)
使用UIAlertView class向用户显示警告消息。
答案 6 :(得分:0)
使用UIAlertView:
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Alert Title"
message:@"are you sure?"
delegate:self
cancelButtonTitle:@"No"
otherButtonTitles:@"Yes", nil];
[av show];
[av autorelease];
确保您实施:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
处理回应。
答案 7 :(得分:0)
要弹出警报消息,请使用UIAlertView。
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"Are you sure you want to delete this." **delegate:self** cancelButtonTitle:@"Delete" otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
将委托设置为self后,您可以对此方法执行操作
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
答案 8 :(得分:0)
由于UIAlertView
已被弃用,我想为未来的编码人员提供答案。
我会像UIAlertView
一样使用UIAlertController
而不是@IBAction func showAlert(_ sender: Any) {
let alert = UIAlertController(title: "My Alert", message: "This is my alert", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: {(action) in
alert.dismiss(animated: true, completion: nil)
}))
self.present(alert,animated:true, completion:nil)
}
:
{{1}}
答案 9 :(得分:-2)
在这里,我提供了警报消息,我在第一个应用程序中使用了该消息:
@IBAction func showMessage(sender: UIButton) {
let alertController = UIAlertController(title: "Welcome to My First App",
message: "Hello World",
preferredStyle: UIAlertControllerStyle.alert)
alertController.addAction(UIAlertAction(title: "OK",
style: UIAlertActionStyle.default,
handler: nil))
present(alertController, animated: true, completion: nil)
}
使用适当的处理程序进行用户响应。