我们过去常常在UIAlertViewDelegate
中使用ViewController.h
来制作我们的自定义提醒按钮来执行操作,例如按下警告窗口中的“再次播放”按钮可以重新启动游戏。
我们如何使用UIAlertController
替代课程UIAlertView
来完成此操作?
我读了Apple提供的这个文件,并没有看到提到的委托方法。这是否意味着我们不再这样做了?
https://developer.apple.com/reference/uikit/uialertcontroller
答案 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提供示例代码,显示热设置。