IOS8 Unified system Hint control--uialertcontroller

Source: Internet
Author: User

IOS8 Unified system Hint control--uialertcontroller

First, Introduction

I believe that in the development of iOS, Uialertview and Uiactionsheet must not be unfamiliar, these two controls in the UI design has played a great role. However, if you have used it, you will find that the two controls have a cumbersome design approach, creating a setup proxy for interface interaction, separating the code logic, and easily creating redundant code. After IOS8, the system attracts uialertcontroller this class, sorting out the two controls, Uialertview and Uiactionsheet, in iOS, if you throw using Uialertview and Uiactionsheet, The system just prompts you to use the new method, IOS9, these two classes are completely deprecated, but this does not mean that the old code will not be used, the old code can still work very well, but there are hidden dangers, uialertcontroller, not only system recommendations, the use of more convenient, more reasonable structure, As a developer, using the new alert controls, we're not doing it. Here's how the old code is used:

Uialertview use: http://my.oschina.net/u/2340880/blog/408873.

Uiactionsheet use: http://my.oschina.net/u/2340880/blog/409907.

Second, the use of Uialertcontroller

From the name of this class we can see that for the alert control, the idea of design is no longer the view but the controller. Exhale through present and push instead of the previous Show method. Another mechanism to change is that the button's triggering method is no longer handled by the agent, but instead encapsulates the button as a class: Uialertaction. The detailed methods and use are as follows:

Uialertcontroller * con = [Uialertcontroller alertcontrollerwithtitle:@ "new" message:@ "looks like" Preferredstyle: Uialertcontrollerstylealert];

[Con addaction:[uialertaction actionwithtitle:@ "Look closely" Style:uialertactionstylecancel handler:^ (UIAlertAction * _ Nonnull action) {

Method of Button triggering

}]];

[Self Presentviewcontroller:con animated:yes completion:nil];

The above code will call out the warning box on the screen as follows:

The Preferref parameter in the initialization method is an enumeration that is determined to be a list of prompts or drawers:

typedef ns_enum (Nsinteger, Uialertcontrollerstyle) {

Uialertcontrollerstyleactionsheet = 0,//Drawer

uialertcontrollerstylealert//Warning Box

}

The Addaction method above adds a button that encapsulates the method, the Uialertaction class is very simple to construct, as follows:

Initialize method

+ (Instancetype) Actionwithtitle: (Nullable NSString *) Title style: (Uialertactionstyle) style handler: (Void (^ __ Nullable) (Uialertaction *action)) handler;

Get title

@property (Nullable, Nonatomic, ReadOnly) NSString *title;

Get style

@property (nonatomic, readonly) Uialertactionstyle style;

setting is valid

@property (nonatomic, getter=isenabled) BOOL enabled;

The alertaction style is enumerated as follows:

typedef ns_enum (Nsinteger, Uialertactionstyle) {

Uialertactionstyledefault = 0,//Default Style

uialertactionstylecancel,//the style of the Cancel button

uialertactionstyledestructive//style of warning

}

The style effect is as follows:

Third, Uialertcontroller other properties and methods

@property (nonatomic, readonly) nsarray<uialertaction *> *actions;

Get all Alertaction

@property (Nonatomic, Strong, nullable) Uialertaction *preferredaction Ns_available_ios (9_0);

The newly added property after iOS9 can make a button more prominent, only set the akertaction already in the actions array, will make the button of the setting more conspicuous, as follows:

-(void) Addtextfieldwithconfigurationhandler: (void (^ __nullable) (Uitextfield *textfield)) Configurationhandler;

Add a TextField, the previous related control, although can also add TextField, but the customization ability is very poor, this new method has a Configurationhandler code block, You can put the relevant setup code of TextField into this block, and the number of TextField added by this method is no longer limited to 2:

[Con addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {

[Email protected] "1th";

}];

[Con addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {

[Email protected] "2nd";

}];

[Con addtextfieldwithconfigurationhandler:^ (Uitextfield * _nonnull TextField) {

[Email protected] "3rd";

}];

@property (Nullable, Nonatomic, ReadOnly) Nsarray<uitextfield *> *textfields;

Get an array of all TextField

@property (nullable, nonatomic, copy) NSString *title;

Set the caption of the alert control

@property (nullable, nonatomic, copy) NSString *message;

Set up information for alert controls

@property (nonatomic, readonly) Uialertcontrollerstyle Preferredstyle;

Get the style of the alert control

IOS8 Unified system Hint control--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.