如果不推荐使用<uialertviewdelegate>,我该如何设置Alert按钮来执行操作?

时间:2016-09-18 16:07:09

标签: ios objective-c delegates uialertcontroller

我们过去常常在UIAlertViewDelegate中使用ViewController.h来制作我们的自定义提醒按钮来执行操作,例如按下警告窗口中的“再次播放”按钮可以重新启动游戏。

我们如何使用UIAlertController替代课程UIAlertView来完成此操作?

我读了Apple提供的这个文件,并没有看到提到的委托方法。这是否意味着我们不再这样做了?

https://developer.apple.com/reference/uikit/uialertcontroller

2 个答案:

答案 0 :(得分:2)

  • 首先添加UIAlertController

  • 然后添加UIAlertAction

  • 然后将action添加到您的alert controller

如下所示。

UIAlertController *myalert = [UIAlertController alertControllerWithTitle:@"your title" message:@"your messate" preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *myaction = [UIAlertAction actionWithTitle:@"you title" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //your action here
    }];


    [myalert addAction:myaction];
    [self presentViewController:myalert animated:YES completion:nil];

答案 1 :(得分:0)

听起来你发现,UIAlertView已被弃用(从iOS 8开始),UIAlertViewDelegate也是如此。

您应该使用UIAlertController代替。 UIAlertController使用不同的方法处理按钮上的用户点击。在UIAlertController中,您将UIAlertAction个对象添加到警报控制器。

UIAlertAction获取在用户触发该操作时调用的代码块。您可以将处理程序块(闭包)传递给您设置的每个按钮操作,而不是设置侦听协议中的消息的委托。这是解决同一问题的另一种方式。

@AnuradhS提供示例代码,显示热设置。

相关问题