UIAlertView 和 UIAlertController [英] UIAlertView And UIAlertController

查看:24
本文介绍了UIAlertView 和 UIAlertController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xcode 6 中使用 SWIFT 来开发我的应用程序并且需要支持 iphone 4 及更高版本的应用程序......所以选择了部署目标"作为 7.1.简单来说就是需要支持iOS7、iOS8和iOS9...

使用警报视图时,我在很多地方都遇到过,现在我们必须使用新引入的UIAlertController"而不是旧的UIAlertView"...

通过阅读了解 UIAlertView 已从 ios 8 中弃用.

但就我而言,因为我必须支持 ios 7.1,而且我不能只使用UIAlertController".

正如许多教程所解释的那样,我开始使用以下方式...

if (objc_getClass("UIAlertController") != nil){//使用 UIAlertController}别的{//使用 UIAlertView}

但是以这种方式必须编写两次相同的代码并且真的很烦人......要么我必须创建一个结合两者的自定义Alertview,要么需要像这样继续编码......

但只是为了测试我只使用了 UIAlertView(忽略 UIAlertController)并且即使在 ios 8 模拟器中该应用程序也能正常运行...但文档说 UIAlertView 已不推荐使用 从 iOS 8.0...

所以我的问题是,想听听通过这些 API 更改继续我的应用程序的最佳做法是什么...如果我忽略UIAlertController"并使用旧的UIAlertView"直到有一天停止对 iOS7 的支持......这会对我的应用程序产生任何不良影响吗?谢谢

解决方案

我为这两个类编写了一个包装器,根据 iOS 版本使用适当的类.

在这里找到

https://github.com/amosavian/ExtDownloader/blob/master/Utility%2BUI.swift

要显示一个简单的警报,请使用:

 Utility.UI.alertShow("message", withTitle: "title", viewController: self);

显示动作视图:使用以下代码:

let buttons = [Utility.UI.ActionButton(title: "Say Hello", buttonType: .Default, buttonHandler: { () -> Void in打印(你好")})]Utility.UI.askAction("message", withTitle: "title", viewController: self, anchor: Utility.UI.AnchorView.BarButtonItem(button: uibarbuttontapped), buttons: buttons)

如果您在 iPhone 中呈现动作,它会自动添加一个取消按钮.但是你可以通过定义一个类型为.Cancel"的按钮来自定义取消按钮

如果您想显示模态警报视图,请使用以下代码:

let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in打印(按下确定按钮")})Utility.UI.askAlert("message", withTitle: "title", viewController: self, 按钮: [cancelBtn, okBtn], textFields: nil)

如您所见,上面的文本字段设置为 nil.如果您想向用户询问一些问题,您可以设置它,如下所示:

let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in打印(您输入了"+ textInputs[0])}})让 textFields = [Utility.UI.AlertTextField(placeHolder: "enter secret text here", defaultValue: "", textInputTraits: TextInputTraits.secretInput())]Utility.UI.askAlert("输入密码", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: textFields)

您几乎可以自定义所有内容.例如,通过定义自定义 textInputTrait,您可以轻松自定义文本输入.你也不必处理委托,而是有简单的闭包.请阅读代码以了解更多信息.

I am using SWIFT in xcode 6 to develope my application AND needs to support the APP for iphone 4 and later versions... So have selected the 'Deploment Target' as 7.1. In simple words needs to support iOS7, iOS8 AND iOS9...

When using Alert View I came across in many places discussing now we have to use newly introduced 'UIAlertController' rather than the old 'UIAlertView'...

By reading got to know UIAlertView is deprecated from ios 8.

But in my case as I have to support for ios 7.1 AND I can NOT only use 'UIAlertController'.

I started using as the following way as many tutorials explains...

if (objc_getClass("UIAlertController") != nil)
{
    // Use UIAlertController
}
else
{
    // Use UIAlertView
}

But in this way got to write the same code twice and really annoyiong... Either I have to create a custom Alertview combining both or needs to continue coding like this....

but just to test I've used only UIAlertView (ignoring UIAlertController) and the app runs fine even in ios 8 simulators... But the document says UIAlertView is deprecated from iOS 8.0...

So my question is, Like to hear what the best practice to continue my app with these API changes... Is it alright if I ignore 'UIAlertController' and work with old 'UIAlertView' until stop support for iOS7 one day... Will that effect in to my app in any way bad? Thanks

解决方案

I've written a wrapper around these two classes which uses appropriate classes according to iOS version.

find it here

https://github.com/amosavian/ExtDownloader/blob/master/Utility%2BUI.swift

to show a simple alert use this:

 Utility.UI.alertShow("message", withTitle: "title", viewController: self);

to show an action view: use this code:

let buttons = [Utility.UI.ActionButton(title: "Say Hello", buttonType: .Default, buttonHandler: { () -> Void in
        print("Hello")
    })]
Utility.UI.askAction("message", withTitle: "title", viewController: self, anchor: Utility.UI.AnchorView.BarButtonItem(button: uibarbuttontapped), buttons: buttons)

In case you are presenting action in iPhone, it will add a cancel button automatically. But you can have custom cancel button by defining a button with type of ".Cancel"

If you want show a modal alert view, use this code:

let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
            print("OK button pressed")
})
Utility.UI.askAlert("message", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: nil)

As you can see, the text fields is set to nil above. You can set it if you want ask something from user, like this:

let cancelBtn = Utility.UI.AlertButton(title: "Cancel", buttonType: .Cancel, buttonHandler: nil)
let okBtn = Utility.UI.AlertButton(title: "OK", buttonType: .Default, buttonHandler: { (textInputs) -> Void in
        print("You entered" + textInputs[0])
    }
})
let textFields = [Utility.UI.AlertTextField(placeHolder: "enter secret text here", defaultValue: "", textInputTraits: TextInputTraits.secretInput())]
Utility.UI.askAlert("Enter password", withTitle: "title", viewController: self, buttons: [cancelBtn, okBtn], textFields: textFields)

You can customize almost everything. e.g by defining custom textInputTrait you can customize text inputs easily. also you have not to deal with delegates, instead simple closures are there. Please read code to find out more.

这篇关于UIAlertView 和 UIAlertController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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