Swift completes the call of UIAlertController
UIAlertView and UIActionSheet in iOS8 have been replaced by UIAlertViewController. Therefore, this blog will discuss how to use swift to generate prompt boxes.
Let's take a look at Apple's UIAlertController document:
import Foundationimport UIKit//// UIAlertController.h// UIKit//// Copyright (c) 2014 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: UIAlertControllerStyle) func addAction(action: UIAlertAction) var actions: [AnyObject] { get } func addTextFieldWithConfigurationHandler(configurationHandler: ((UITextField!) -> Void)!) var textFields: [AnyObject]? { get } var title: String? var message: String? var preferredStyle: UIAlertControllerStyle { get }}
We can see that there are two styles in UIAlertController, one is ActionSheet, the other is Alert, and the AlertActionStyle has three: Default, Cancel, Destructive; so we can do this when creating Alert:
Var alert: UIAlertController = UIAlertController (title: nil, message: "the phone number you entered is incorrect. Check the phone number and enter", preferredStyle: UIAlertControllerStyle. Alert)
Or
var alert: UIAlertController = UIAlertController(title: nil, message:"test", preferredStyle: UIAlertControllerStyle.ActionSheet)
Next, we will add an action to Alert. From the document, we can see that AlertAction has the init function,
Let's create three 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 the handler uses a closure
Then add actions to alertcontroller and display it.
alert.addAction(saveAction) alert.addAction(deleteAction) alert.addAction(cancelAction) self.presentViewController(alert, animated: true, completion: nil)
You can also add an action.
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 ")}}))
Run the following command to see what alertController looks like.
Tips:
If the style is cancel, the font will become coarse. If the style is destructive, the font will be red.