如何在iPhone中创build一个警告框?

我想创build一个警告types的框,以便当用户试图删除某些东西时,它会说:“你确定吗?”,如果他们确定的话,那么就会有“是”或“否”。 什么是最好的方式来做到这一点在iPhone?

UIAlertView是最好的方式来做到这一点。 它会在屏幕中间生成animation,调暗背景,并强制用户解决问题,然后再返回到应用程序的正常function。

你可以像这样创build一个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>放在您的类inheritance的任何位置(即UIViewControllerUITableViewController等)

有关UIAlertViews所有细节的更多信息,请查看Apple的Docs Here

希望有所帮助

这个职位相当老,但仍然是一个很好的问题。 随着iOS 8的答案已经改变。 今天,你宁愿使用“UIAlertController”'UIAlertControllerStyle.ActionSheet'的'preferredStyle'。

这样的代码(swift)绑定到一个button:

 @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 +。 如果添加必要的行,如下所述: 在另一个堆栈溢出答案

为了迅速,它非常简单。

  //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) 

参考: iOS显示警告消息

每个人都在说UIAlertView。 但要确认删除,UIActionSheet可能是更好的select。 请参阅何时使用UIAlertView与UIActionSheet

UIAlertView似乎是确认的明显select。

将代理设置为控制器并实现UIAlertViewDelegate协议http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

使用UIAlertView类向用户显示警报消息。

使用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 

处理响应。

要popup警报消息,请使用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]; 

一旦您将委托设置为自我,您可以对此方法执行操作

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex