Ios:uialertcontroller and Uialertaction's explanation

Source: Internet
Author: User

Cue box controller: Uialertcontroller

Prompt Box button: uialertaction  function: used to prompt the information, and give some can choose the button to handle the corresponding requirements.  Note:In Xcode's IOS8 SDK, Uialertview and Uiactionsheet are replaced by Uialertcontroller. The official library explains: "Uialertview is deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstylealert instead. "," Uiactionsheet is deprecated. Use Uialertcontroller with a preferredstyle of Uialertcontrollerstyleactionsheet instead. ". Illustrates the development of ios8+, Uialertview and Uiactionsheet are obsolete, uialertcontroller in a modular replacement to replace these two control functions and roles. How to create and use Uialertcontroller becomes our concern.  class Description: 1, Cue box style enumeration (divided into Uialertview, uiactionsheet)

typedef ns_enum (Nsinteger, Uialertcontrollerstyle) {

Uialertcontrollerstyleactionsheet = 0, //Cue box pops up at the bottom of the view

Uialertcontrollerstylealert //Popup box in the middle of the view

} ns_enum_available_ios (8_0);

2 . The style of the button on the Cue box

typedef ns_enum (Nsinteger, Uialertactionstyle) {

Uialertactionstyledefault = 0, //Default Confirmation button

Uialertactionstylecancel, //default Cancel button

Uialertactionstyledestructive //Default red button

}ns_enum_available_ios (8_0);

3. uialertcontroller: Cue box controller class

@interface Uialertcontroller:uiviewcontroller

Method:

To create a class method for a cue box controller

+ (Instancetype) Alertcontrollerwithtitle: (NSString *) title message: (NSString *) message Preferredstyle: ( Uialertcontrollerstyle) Preferredstyle;

Add an instance method of the text box on the prompt (only in the Uialertview style hint box)

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

Add a button to the prompt box

-(void) Addaction: (uialertaction *) action;

Property:

An array of all the buttons on the Cue box

@property (nonatomic, readonly) Nsarray *actions;

An array of all text boxes on the prompt box

@property (nonatomic, readonly) Nsarray *textfields;

Title of the Cue box

@property (nonatomic, copy) NSString *title;

Prompt information

@property (nonatomic, copy) NSString *message;

Style of the Cue box controller

@property (nonatomic, readonly) Uialertcontrollerstyle Preferredstyle;

@end

4. Uialertaction: Prompt button

@interface Uialertaction:nsobject <NSCopying>

Method:

class method for creating a Cue box button

+ (Instancetype) Actionwithtitle: (NSString *) Title style: (Uialertactionstyle) style handler: (void (^) (uialertaction * Action)) handler;

Property:

Button title

@property (nonatomic, readonly) NSString *title;

The style of the button

@property (nonatomic, readonly) Uialertactionstyle style;

Whether the button is valid

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

@end

The specific examples are as follows:

To create a step:

1. Layout storyboard, drag a button in the view of the controller, and associate the Ibaction event

2. The main code in the associated event of the button is as follows:

Create a cue box controller

    // Create a cue box controller    Uialertcontroller *alertcontroller = [Uialertcontroller alertcontrollerwithtitle:@ " prompt box " Message:@ " messages "  Preferredstyle:uialertcontrollerstylealert];     = [Uicolor Purplecolor];

Create a prompt Box button

    // Create a prompt button    Uialertaction *action1 = [uialertaction actionwithtitle:@ " default Cancel"  style: Uialertactionstylecancel Handler:nil];         *action2 = [uialertaction actionwithtitle:@ ' default'  style: Uialertactionstyledefault Handler:nil];         *action3 = [uialertaction actionwithtitle:@ " default destructive" style: Uialertactionstyledestructive Handler:nil];

Add a hint button to the prompt box

    // Add a hint button     [Alertcontroller addaction:action1];    [Alertcontroller Addaction:action2];    [Alertcontroller Addaction:action3];

Add a text box to the Cue box (only for the cue box style: Uialertcontrollerstylealert)

    // Add a text box (only for Alertview type of cue box)    [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textField) {        @ " account ";    }];    [Alertcontroller Addtextfieldwithconfigurationhandler:^ (Uitextfield *textField) {        @ "  password ";         // Safe Input mode    }];

Add a listen event to a text box

    // Add a listen event to a text box (start, End, state change, etc.)    [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textField) {        @ "  Add Listener Event ";                [TextField addtarget:self Action: @selector (alerttextfileddidchanged:) forControlEvents: Uicontroleventeditingchanged];    }];

Display a cue box as a modal window

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

Implementing a text Box event

#pragma mark text Box Listener event-(void) alerttextfileddidchanged: (nsnotification *) notification{    NSLog (@ "enditing changed");}

Click the button to display the results of the presentation

When the Action3 button is not added to the cue box, which is the number of buttons <=2, the style of the two prompts is:

Uialertcontrollerstylealert: Pop up from the middle of the screen

Uialertcontrollerstyleactionsheet: Pop-up from the bottom of the screen (cannot add a text box)

When the Action3 button is not added to the cue box, which is the number of buttons >=3, the style of the two prompts is:

Uialertcontrollerstylealert: Pop up from the middle of the screen

Uialertcontrollerstyleactionsheet: Pop-up from the bottom of the screen (cannot add a text box)

Ios:uialertcontroller and Uialertaction in a detailed

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.