The use of Swift-uialertcontroller

Source: Internet
Author: User

iOS 8 after the official Apple recommended the use of Uialertcontroller this class, so specifically to find information online, understand the usage, write a code, record!

Thank you, turn from: http://www.hangge.com/blog/cache/detail_651.html

1. Create a Alertview

 Overridefunc viewdidappear (animated:bool) {super.viewdidappear (animated) let Alertcontroller= Uialertcontroller (title:"System Tips", message:"Are you sure you want to leave hangge.com? ", Preferredstyle:. Alert) Let Cancelaction= Uialertaction (title:"Cancel", Style:. Cancel, Handler:nil) let Okaction= Uialertaction (title:"yes, okay.", Style:. Default, Handler: {actioninchPrint ("clicked OK.")}) alertcontroller.addaction (cancelaction) alertcontroller.addaction (okaction) self.pres Entviewcontroller (Alertcontroller, animated:true, Completion:nil)}

2. Create a Actionsheet

(Note: If there is a "cancel" button on the menu, it will always appear in the menu, regardless of the order in which it was added)

Let Alertcontroller = Uialertcontroller (title:"Save or delete data", Message:"Delete data will not be recoverable", Preferredstyle:. Actionsheet) Let Cancelaction= Uialertaction (title:"Cancel", Style:. Cancel, Handler:nil) let Deleteaction= Uialertaction (title:"Delete", Style:. Destructive, Handler:nil) let Archiveaction= Uialertaction (title:"Save", Style:. Default, Handler:nil) alertcontroller.addaction (cancelaction) alertcontroller.addaction (deleteaction) Alertcontroller.addaction (archiveaction) Self.presentviewcontroller (Alertcontroller, animated:true, Completion:nil)

3, button use warning mode, text color change, used to alert users

var " yes, okay. ", Style:UIAlertActionStyle.Destructive, Handler:nil)

4. Add any number of text input boxes

Overridefunc viewdidappear (animated:bool) {super.viewdidappear (animated) let Alertcontroller= Uialertcontroller (title:"System Login", message:"Please enter user name and password", PreferredStyle:UIAlertControllerStyle.Alert) Alertcontroller.addtextfieldwithconfigurationhandler { (Textfield:uitextfield!) -VoidinchTextfield.placeholder="User name"} alertcontroller.addtextfieldwithconfigurationhandler {(Textfield:uitextfield!) -VoidinchTextfield.placeholder="Password"Textfield.securetextentry=true} alertcontroller.addtextfieldwithconfigurationhandler {(Textfield:uitextfield)->voidinchTextfield.placeholder="Duplicate Password"Textfield.securetextentry=trueprint (Textfield.text)} Let Cancelaction= Uialertaction (title:"Cancel", Style:. Cancel, Handler:nil) let Okaction= Uialertaction (title:"yes, okay.", Style:. Default, Handler: {actioninch                //you can also get TextField let login = alertcontroller.textfields! in the form of subscript [0]Let login = alertcontroller.textfields!. first! asUitextfield Let password= alertcontroller.textfields!. last! asUitextfield Print ("User name: \ (login.text) password: \ (password.text)")}) alertcontroller.addaction (cancelaction) alertcontroller.addaction (okaction) self.pres Entviewcontroller (Alertcontroller, animated:true, Completion:nil)}

5. Use code to remove the prompt box

Self.presentedviewcontroller?. Dismissviewcontrolleranimated (false, Completion:nil)

The use of Swift-uialertcontroller

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.