IOS8 using Uialertcontroller (Uialertview and Uiactionsheet two in the same)

Source: Internet
Author: User

IOS8 introduced a few new "controller", mainly to the similar before the Uialertview into the Uialertcontroller, this casual change, it seems that I understand before the "controller" suddenly overturned ~ but it does not matter, There is no fear of new things, learn to use on the line. The next step is to explore these new controllers.

-(void) Showokaycancelalert {nsstring *title = nslocalizedstring (@ "A short title was best", nil);    NSString *message = nslocalizedstring (@ "a message should is a short, complete sentence.", nil);    NSString *cancelbuttontitle = nslocalizedstring (@ "Cancel", nil);    NSString *otherbuttontitle = nslocalizedstring (@ "OK", nil); Uialertcontroller *alertcontroller = [Uialertcontroller alertcontrollerwithtitle:title message:message        Preferredstyle:uialertcontrollerstylealert];    Create the actions. Uialertaction *cancelaction = [Uialertaction actionwithtitle:cancelbuttontitle style:uialertactionstylecancel        handler:^ (uialertaction *action) {NSLog (@ "the \" okay/cancel\ "alert ' s Cancel action occured.");}]; Uialertaction *otheraction = [uialertaction actionwithtitle:otherbuttontitle style:uialertactionstyledefault Handler        : ^ (uialertaction *action) {NSLog (@ "the \" okay/cancel\ "alert ' s other action occured.");}];    Add the actions. [AlertcoNtroller Addaction:cancelaction];        [Alertcontroller addaction:otheraction]; [Self Presentviewcontroller:alertcontroller animated:yes completion:nil];}

This is the most common one alertcontroller, a Cancel button, and a OK button.

The new Alertcontroller, its initialization method is not the same, the button response method binding uses the Block method, both pros and cons. It is important to note that you should not use __weak, especially to self, because the block causes a reference loop.

The above interface is as follows:

If Uialertaction *otheraction This otheraction a few more words, it will automatically be arranged as follows:

In addition, many times, we need to add an input box in Alertcontroller, for example, enter a password:

You can add the following code:

[Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {        //You can customize the TextField here, such as changing the background color        textfield.backgroundcolor = [Uicolor orangecolor];    }];

and changing the background color would be like this:

Full Password Input:

-(void) Showsecuretextentryalert {nsstring *title = nslocalizedstring (@ "A short title was best", nil);    NSString *message = nslocalizedstring (@ "a message should is a short, complete sentence.", nil);    NSString *cancelbuttontitle = nslocalizedstring (@ "Cancel", nil);    NSString *otherbuttontitle = nslocalizedstring (@ "OK", nil); Uialertcontroller *alertcontroller = [Uialertcontroller alertcontrollerwithtitle:title message:message        Preferredstyle:uialertcontrollerstylealert];    Add the text field for the secure text entry. [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {//Listen for changes-the text field ' s text so, we can toggle the current//action ' s enabled Prope        Rty based on whether the user has entered a sufficiently//secure entry. [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector ( Handletextfieldtextdidchangenotification:) name:uitextfieldtextdidchangenotification objecT:textfield];    Textfield.securetextentry = YES;    }];    Create the actions. Uialertaction *cancelaction = [Uialertaction actionwithtitle:cancelbuttontitle style:uialertactionstylecancel        handler:^ (uialertaction *action) {NSLog (@ "the \" Secure Text entry\ "alert ' Cancel action occured.");        Stop listening for text changed notifications. [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uitextfieldtextdidchangenotification object:    AlertController.textFields.firstObject];    }]; Uialertaction *otheraction = [uialertaction actionwithtitle:otherbuttontitle style:uialertactionstyledefault Handler        : ^ (uialertaction *action) {NSLog (@ "the \" Secure Text entry\ "alert ' s other action occured.");        Stop listening for text changed notifications. [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uitextfieldtextdidchangenotification object:    AlertController.textFields.firstObject];        }]; The text field InitiaLly have no text in the text field, so we'll disable it.    otheraction.enabled = NO;    Hold onto the secure text alert action to toggle the enabled/disabled state when the text changed.    Self.securetextalertaction = otheraction;    Add the actions.    [Alertcontroller addaction:cancelaction];        [Alertcontroller addaction:otheraction]; [Self Presentviewcontroller:alertcontroller animated:yes completion:nil];}

Note Four points:

1. Add notifications to monitor changes in TextField content:

Add the text field for the secure text entry.    [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield) {        //Listen for changes-the text field ' s text so, we can toggle the current        //action ' s enabled property Ba Sed on whether the user have entered a sufficiently        //secure entry.        [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector ( Handletextfieldtextdidchangenotification:) name:uitextfieldtextdidchangenotification Object:textField];        Textfield.securetextentry = YES;    }];

2. When initializing, disable the "OK" button:

otheraction.enabled = NO;

Self.securetextalertaction = otheraction;//defines a global variable to store

3. When entering more than 5 characters, make self.securetextalertaction = YES:

-(void) Handletextfieldtextdidchangenotification: (nsnotification *) notification {    Uitextfield *textfield = Notification.object;    Enforce a minimum length of >= 5 characters for secure text alerts.    self.secureTextAlertAction.enabled = TextField.text.length >= 5;}

4. Remove the notification from the "OK" action:

Uialertaction *otheraction = [uialertaction actionwithtitle:otherbuttontitle style:uialertactionstyledefault Handler : ^ (uialertaction *action) {        NSLog (@ "the \" Secure Text entry\ "alert ' s other action occured.");        Stop listening for text changed notifications.        [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uitextfieldtextdidchangenotification object: AlertController.textFields.firstObject];    }];

The last is often used in conjunction with the Alertview and Actionsheet, and here too:

-(void) Showokaycancelactionsheet {nsstring *cancelbuttontitle = nslocalizedstring (@ "Cancel", nil);        NSString *destructivebuttontitle = nslocalizedstring (@ "OK", nil); Uialertcontroller *alertcontroller = [Uialertcontroller alertcontrollerwithtitle:nil message:nil PreferredStyle:        Uialertcontrollerstyleactionsheet];    Create the actions. Uialertaction *cancelaction = [Uialertaction actionwithtitle:cancelbuttontitle style:uialertactionstylecancel handler:^ (uialertaction *action) {NSLog (@ "the \" okay/cancel\ "alert action sheet ' Cancel action occured.");        ]; Uialertaction *destructiveaction = [uialertaction actionwithtitle:destructivebuttontitle style: Uialertactionstyledestructive handler:^ (uialertaction *action) {NSLog (@ "the \" okay/cancel\ "alert action sheet ' s D        Estructive action occured. ");}];    Add the actions.    [Alertcontroller addaction:cancelaction];        [Alertcontroller addaction:destructiveaction]; [Self PRESENTVIEwcontroller:alertcontroller animated:yes Completion:nil];} 

The bottom is displayed as follows:

Well, at this point, basically know how the new controller is actually used.

IOS8 using Uialertcontroller (Uialertview and Uiactionsheet two in the same)

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.