从通用实用程序类显示 UIAlertController [英] Show UIAlertController from a common utility class

查看:22
本文介绍了从通用实用程序类显示 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 UIAlertView 的项目,现在的问题是我必须用 UIAlertController's 替换所有的 UIAlertView's 并在那里在代码中大约有 1250 个.我打算使用现有的 Utility 类来创建一个执行此操作的函数,以下是我打算做的:

I am working on a project that uses UIAlertView, now the problem is I have to replace all of the UIAlertView's with UIAlertController's and there are around 1250 of them in the code. I am planning to uses the existing Utility class to create a function that does this, following is what I am planning to do:

+(void)showAlert:(NSString *)title errorMessage:(NSString *)msg {
    UIAlertController *alertController = [UIAlertController 
    alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alertController animated:YES completion:nil];
}

在执行此操作之前,我有以下问题:1 - 这值得付出努力吗(使用 UIAlertController 而不是 UIAlertView)?2 - 如何处理在数百个文件中具有标签和不同委托实现的 UIAlertView's ?3 - 上述函数的第四行给出错误:No known class method for selector 'presentViewController:animated:completion:'

I have the following questions before doing this: 1 - Is this worth the efforts (Using UIAlertController instead of UIAlertView) ? 2 - How do I handle UIAlertView's that had tags and different delegate implementation across hundreds of files ? 3 - The fourth line the function above gives an error : No known class method for selector 'presentViewController:animated:completion:'

非常感谢任何帮助.

推荐答案

您必须使用 UIAlertController,因为 UIAlertView 已被弃用.

You've to use UIAlertController as UIAlertView is deprecated.

Apple 文档说:

在 iOS 8 之前的 iOS 版本中运行的应用中,使用UIAlertView 类向用户显示警报消息.警报视图功能与动作相似但外观不同sheet(UIActionSheet 的一个实例).UIAlertView 在 iOS 中已被弃用8.(请注意 UIAlertViewDelegate 也已弃用.)要在 iOS 8 及更高版本中创建和管理警报,请使用带有UIAlertControllerStyleAlert 的首选样式.可用性

In apps that run in versions of iOS prior to iOS 8, use the UIAlertView class to display an alert message to the user. An alert view functions similar to but differs in appearance from an action sheet (an instance of UIActionSheet). UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated.) To create and manage alerts in iOS 8 and later, instead use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert. Availability

或者,您可以这样做.在您的 Utils 类中,执行以下操作:

Alternatively, you can do this way. In your Utils Class, do this:

+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)msg controller:(id)controller {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];

    [alertController addAction:okAction];

    [controller presentViewController:alertController animated:YES completion:nil];
}

来自您的 ViewController 类的用法:

Usage from your ViewController class:

[Utils showAlertWithTitle:@"Camera" message:@"It seems that your device doesn't support camera. " controller: self];

如果您现有的 UIAlertView 有标签,那么您必须使用 UIAlertViewController 类而不是 Util 方法.

If your existing UIAlertView is having tags then you've to use the UIAlertViewController class instead of the Util method.

希望有帮助.

这篇关于从通用实用程序类显示 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆