IOS iOS8 new Features-uialertcontroller

Source: Internet
Author: User

IOS iOS8 new Features--uialertcontroller

1. IOS7 and IOS7 warning class controls are used with Uialertview and UIActionSheet1.1 Uialertview

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Warning" message:@ "This is a Uialertview" Delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "off", nil];

Alert.alertviewstyle = Uialertviewstyleplaintextinput;

The enumeration values for/** Alertviewstyle are as follows

Uialertviewstyledefault = 0,

Uialertviewstylesecuretextinput,

Uialertviewstyleplaintextinput,

Uialertviewstyleloginandpasswordinput

*/

[Alert show];

Uialertviewdelegate

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) Buttonindex;

Use of 1.2 Uiactionsheet

Uiactionsheet *sheet = [[Uiactionsheet alloc] initwithtitle:@ "This is a Uiactionsheet" Delegate:self cancelbuttontitle:@ "Cancel "Destructivebuttontitle:@" OK "

otherbuttontitles:@ "Off", nil];

[Sheet ShowInView:self.view];

Uiactionsheetdelegate

-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex

2. Starting with iOS8, use Uialertcontroller instead of uialertview+uiactionsheet2.1 Uialertcontroller (Style: Uialertcontrollerstylealert)

Initialize Uialertcontroller

Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "Warning" message:@ "ios new control UIAlertController" Preferredstyle:uialertcontrollerstylealert];

Add a button, note if the block's circular reference

__weak typeof (Alert) Weakalert = alert;

[Alert addaction:[uialertaction actionwithtitle:@ "OK" style:uialertactionstyledestructive handler:^ (UIAlertAction * Action) {

What to do after the block internal write-click button

NSLog (@ "Clicked OK button--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);

}]];

Add a text box

[Alert addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {

Textfield.textcolor = [Uicolor Bluecolor];

Textfield.text = @ "Please enter user name";

[TextField addtarget:self Action: @selector (Usernamedidchange:) forcontrolevents:uicontroleventeditingchanged];

You can also use notifications to monitor TextField changes.

[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (usernamedidchange:) Name: Uitextfieldtextdidchangenotification Object:textfield];

}];

[Alert addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {

Textfield.securetextentry = YES;

Textfield.text = @ "123456";

}];

Eject Uialertcontroller

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

TextField Editingchanged, the callback method

-(void) Usernamedidchange: (Uitextfield *) Username

{

NSLog (@ "%@", Username.text);

}

Use of 2.2 Uialertcontroller (Style:uialertcontrollerstyleactionsheet)

On the 2.1 code, modify the code that initializes the Uialertcontroller

Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "Warning" message:@ "ios new control UIAlertController" Preferredstyle:

Uialertcontrollerstyleactionsheet];

After running the program error, because the text field can only be used on Style:uialertcontrollerstylealert uialertcontroller, add textfiled code to write off, and then run the program is normal

Reason: ' Text fields can-only is added to an alert controller of style Uialertcontrollerstylealert '

2.3 Through the above introduction, you can see that Apple intends toUialertview,uiactionsheet Unified, in fact, Apple also unified the iphone and ipad uialertcontroller processing Way

2.3.1 When creating a new project, devices selects the universal, the language chooses OC (error)

2.3.2 in Main.storyboard to make Mainviewcontroller a navigation controller Rootviewcontroller

2.3.3 code Example

Initialize Uialertcontroller

Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "Warning" message:@ "ios new control UIAlertController" Preferredstyle:

Uialertcontrollerstylealert];

Set the item that PopOver points to

Alert.popoverPresentationController.barButtonItem = Self.navigationItem.leftBarButtonItem;

Add button

[Alert addaction:[uialertaction actionwithtitle:@ "OK" style:uialertactionstyledestructive handler:^ (UIAlertAction * Action) {

NSLog (@ "Click OK button");

}]];

[Alert addaction:[uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ (UIAlertAction * Action) {

NSLog (@ "Click the Cancel button");

}]];

Eject Uialertcontroller

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

Note: The image on the left is running on the iphone, the right image is the effect on the ipad, the style is Uialertcontrollerstylealert, the display styles are consistent

2.3.4 Change the 2.3.3 initialization code to Style:uialertcontrollerstyleactionsheet

Uialertcontroller *alert = [Uialertcontroller alertcontrollerwithtitle:@ "Warning" message:@ "ios new control UIAlertController" Preferredstyle:uialertcontrollerstyleactionsheet];

Note: The image on the left is the iphone and the ipad on the right is a different display style for the iphone and ipad with a single code.

IOS iOS8 new Features-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.