Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iphone中创建警报框?_Iphone_Objective C_Ios_Uialertview - Fatal编程技术网

如何在iphone中创建警报框?

如何在iphone中创建警报框?,iphone,objective-c,ios,uialertview,Iphone,Objective C,Ios,Uialertview,我想创建一个警报类型框,这样当用户试图删除某个内容时,它会说“你确定吗”,然后如果他们确定,就会有一个“是”或“否”。在iphone上实现这一点的最佳方式是什么?UIAlertView似乎是显而易见的确认选择 将代理设置为控制器并实施UIAlertViewDelegate协议使用向用户显示警报消息。使用: 确保您实施: - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 处

我想创建一个警报类型框,这样当用户试图删除某个内容时,它会说“你确定吗”,然后如果他们确定,就会有一个“是”或“否”。在iphone上实现这一点的最佳方式是什么?

UIAlertView似乎是显而易见的确认选择

将代理设置为控制器并实施UIAlertViewDelegate协议使用向用户显示警报消息。

使用:

确保您实施:

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

处理响应。

一个
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
放在类继承的对象旁边(即
UIViewController
UITableViewController
等),以包括
UIAlertViewDelegate

有关
UIAlertViews
的所有详细信息,请查看


希望这对大家的看法有所帮助。但要确认删除,UIActionSheet可能是更好的选择。请参见使用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

这个帖子很老了,但还是个好问题。有了iOS 8,答案就变了。今天,您更希望将“UIAlertController”与“UIAlertControllerStyle.ActionSheet”的“preferredStyle”一起使用

绑定到按钮的类似于(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+上崩溃。如果按此处所述添加必要的行:

对于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)

Ref:

由于
UIAlertView
现在已被弃用,我想为未来遇到这种情况的程序员提供一个答案

我将使用
UIAlertController
而不是
UIAlertView
,如下所示:

@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)
}

在这里,我提供了我在第一个应用程序中使用的警报消息:

@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)
}

为用户响应提供适当的处理程序。

@一二三: 你有超过1000名代表;如果您不编辑问题内容,是否有一个“重新标记”链接可用于重新标记问题而无需经过批准?您是否可以添加每个不同样式的示例?我知道用户要求删除,但看到其他类型的UIAlertControllersR(也就是大家都在谈论的好的旧UIAlertView:d)Hi@Zil没问题。我添加了第三种动作样式(默认样式)。我没有其他首选样式的经验,但您可以轻松地获取代码片段并进行尝试。如果您能找到商店中的哪个应用程序,您将收到一份beta tester的邀请;-)这是不推荐的,我们应该使用UIAlertController
@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)
}