Swift completes the Uialertcontroller call

Source: Internet
Author: User
Tags uikit

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

Related Article

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.