UIAlertView And UIAlertController

时间:2015-09-14 15:45:27

标签: ios swift uialertview uialertcontroller

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

1 个答案:

答案 0 :(得分:0)

我已经围绕这两个类编写了一个包装器,它根据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
        print("Hello")
    })]
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
            print("OK button pressed")
})
Utility.UI.askAlert("message", withTitle: "title", viewController: self, buttons: [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
        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)

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