Swift completes the call of UIAlertController

Source: Internet
Author: User

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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.