IOS8 in the Uialertview and Uiactionsheet have been replaced by Uialertviewcontroller, so, this blog to explore how to use Swift to generate the prompt box.
Let's start by looking at Apple's Uialertcontroller documentation:
Import foundationimport uikit////uialertcontroller.h//uikit////Copyright (c) Apple Inc. All Rights reserved.//@availability (IOS, introduced=8.0) enum Uialertactionstyle:int {case Default case Cancel Case destructive} @availability (IOS, introduced=8.0) enum Uialertcontrollerstyle:int {case Actionsheet case Alert} @availability (IOS, introduced=8.0) class Uialertaction:nsobject, nscopying {convenience init (title:string , Style:uialertactionstyle, Handler: ((uialertaction!)-Void)!) var title:string {get} var style:uialertactionstyle {get} var Enabled:bool} @availability (IOS, introduced=8.0) Class Uialertcontroller:uiviewcontroller {convenience init (title:string, message:string?, Preferredstyle:uia Lertcontrollerstyle) func addaction (Action:uialertaction) var actions: [Anyobject] {get} func Addtextfield Withconfigurationhandler (Configurationhandler: ((uitextfield!)-Void)!) var TextfielDS: [Anyobject]? {get} var title:string? var message:string? var preferredstyle:uialertcontrollerstyle {get}}
We can see that there are two style uialertcontroller, one is Actionsheet, one is alert, and Alertactionstyle has 3: default,cancel, destructive So we can do this when we create a new alert:
var Alert:uialertcontroller = Uialertcontroller (title:nil, message: "You entered the wrong phone number, please check and re-enter", Preferredstyle: Uialertcontrollerstyle.alert)
or
var Alert:uialertcontroller = Uialertcontroller (title:nil, message: "Test", Preferredstyle: Uialertcontrollerstyle.actionsheet)
Next we'll add an action to alert, and from the document we can see that alertaction has the init function,
Let's create a new 3 actions.
var saveaction = uialertaction (title: "Save", Style:.) Default, handler:{ (alerts:uialertaction!), Void in println ("File Saved") }) var deleteaction = Uialertaction (title: "Delete", Style:.) Default, handler:{ (alerts:uialertaction!), Void in println ("File delete") }) var cancelaction = Uialertaction (title: "Cancel", Style:.) Cancel, handler:{ (alerts:uialertaction!), Void in println ("Cancelled") })
Notice that a closure is used in the handler.
Then add the actions to our Alertcontroller and show it.
Alert.addaction (saveaction) alert.addaction (deleteaction) alert.addaction (cancelaction) Self.presentviewcontroller (Alert, animated:true, Completion:nil)
We can also add action like this
Alert.addaction (uialertaction (title: "OK", Style:.) Destructive, handler: { action in switch action.style{case . Default: println ("OK") case . Cancel: println ("Cancel") case . Destructive: println ("destructive") } ) )
Let's run a little bit to see what our alertcontroller look like.
Tips:
If the style is cancel then the font will be thicker, and if it is destructive, the font will appear red.
Swift completes the Uialertcontroller call