IOS SDK Detailed Uialertcontroller (IOS8 replaces Alertview and Actionsheet)

Source: Internet
Author: User

Original blog, reproduced please indicate the source
Blog.csdn.net/hello_hwc

Two months or so, no iOS project has been developed for the company (IoT has been going on lately) so much so that this update to iOS 8 is not seen. Fill it up here.

An overview

After the IOS8, Uialertcontroller replaced Uiactionsheet and Uialertview. Put two types of cue information into this class to implement.
Note that this class cannot be defined in the way it inherits .

Introduction to the second class

Let's give two examples of use
Example One

Uialertcontroller * Alertcontroller = [Uialertcontroller alertcontrollerwithtitle:NilMessageNilPreferredstyle:uialertcontrollerstyleactionsheet];//Add button[Alertcontroller addaction: [uialertaction actionwithtitle: @"Take photos"Style:uialertactionstyledefault handler:^ (uialertaction *action) {//Process Click to take photos}]]; [Alertcontroller addaction: [uialertaction actionwithtitle: @"Select from album"Style:uialertactionstyledefault handler:^ (uialertaction *action) {//Processing Click Select from album}]]; [Alertcontroller addaction: [uialertaction actionwithtitle: @"Cancel"Style:uialertactionstylecancel handler:Nil]]; [ SelfPresentviewcontroller:alertcontroller Animated:YESCompletion:Nil];

Example Two

Implementation code

Uialertcontroller * Alertcontroller = [Uialertcontroller alertcontrollerwithtitle: @"Login"Message: @"Enter user name password"Preferredstyle:uialertcontrollerstylealert];[Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {TextField. Placeholder= @"Name";TextField. TextColor= [Uicolor Bluecolor];TextField. Clearbuttonmode= Uitextfieldviewmodewhileediting;TextField. BorderStyle= Uitextborderstyleroundedrect;}];[Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {TextField. Placeholder= @"Password";TextField. TextColor= [Uicolor Bluecolor];TextField. Clearbuttonmode= Uitextfieldviewmodewhileediting;TextField. BorderStyle= Uitextborderstyleroundedrect;TextField. Securetextentry= YES;}];[Alertcontroller addaction:[uialertaction actionwithtitle:@"OK"Style:uialertactionstyledefault handler:^ (uialertaction *action) {Nsarray * Textfields = AlertController. Textfields;Uitextfield * Namefield = textfields[0];Uitextfield * passwordfiled = textfields[1];NSLog (@"%@:%@", Namefield. Text, passwordfiled. Text);}]];[Self Presentviewcontroller:alertcontroller animated:yes Completion:nil];
Three steps to use

First step initialization

+ (instancetype)alertControllerWithTitle:(NSString *)title                                 message:(NSString *)message                          preferredStyle:(UIAlertControllerStyle)preferredStyle

There are two kinds of preferredstyle here, sheet and alert.

typedefenumNSInteger {   0,   UIAlertControllerStyleAlert } UIAlertControllerStyle;

Second step, add action (button or TextField)
Add button
-(void) Addaction: (uialertaction *) action
The uialertaction here is a relatively simple class

+ (instancetype)actionWithTitle:(NSString *)title                          style:(UIAlertActionStyle)style                        handler:(void (^)(UIAlertAction *action))handler

There are three types of style

typedefenumNSInteger {   0,//默认   UIAlertActionStyleCancel,//取消   //有可能改变或者数据} UIAlertActionStyle;

Add TextField
Note that it can only be uialertcontrollerstylealert to add TextField

- (void)addTextFieldWithConfigurationHandler:(void (^)(UITextField *textField))configurationHandler

Configure TextField information, such as Placeholder,backgroundcolor, in the block.
The saved information for TextField can be obtained from the Uialertcontroller property textfields. As in the above example two.

The third step shows
For example

[self presentViewController:alert animated:YES completion:nil];
Summarize:

In general, the API unifies the two types of Alertview and does not use proxies to deliver messages. Instead, it changed to block. Personally, it's more like the way block is. Please ignore the background of the above two pictures, I am going to write another demo project about the photo blog.

IOS SDK Detailed Uialertcontroller (IOS8 replaces Alertview and Actionsheet)

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.