The Uialertcontroller of the iOS8 of iOS8 development

Source: Internet
Author: User

Use Uiactionsheet and Uialertview to provide button selection and informational information before iOS8, such as Uiactionsheet can write:

Uiactionsheet *actionsheet = [[Uiactionsheet alloc]                                    initwithtitle:@ "Title,nil not Displayed"                                    delegate:self                                    cancelbuttontitle:@ "Cancel"                                    destructivebuttontitle:@ "OK"                                    otherbuttontitles:@ "First", @ "second", Nil];      Actionsheet.actionsheetstyle = Uiactionsheetstyleblackopaque;      [Actionsheet ShowInView:self.view];

Then implement the proxy in the protocol:

(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (nsinteger) buttonindex  {      if ( Buttonindex = = 0) {          NSLog (@ "OK");      } else if (Buttonindex = = 1) {          NSLog (@ "first item");      } else if (Buttonindex = = 2) {          NSLog (@ "second item");      } else if (Buttonindex = = Actionsheet.canclebuttonindex) {          NSLog (@ "Cancel");}     }  -(void) Actionsheetcancel: (Uiactionsheet *) actionsheet{      }    -(void) Actionsheet: (Uiactionsheet *) Actionsheet Diddismisswithbuttonindex: (Nsinteger) buttonindex{      }    -(void) Actionsheet: (Uiactionsheet *) Actionsheet Willdismisswithbuttonindex: (Nsinteger) buttonindex{      }  

If you need to modify the button font, color, etc., you can implement the following proxies:

-(void) Willpresentactionsheet: (Uiactionsheet *) Actionsheet {for (UIView *subviwe in actionsheet.subviews) {if ([Subviwe Iskindofclass:[uilabel class]])            {UILabel *label = (UILabel *) Subviwe;            Label.font = [Uifont systemfontofsize:16]; Label.frame = CGRectMake (Cgrectgetminx (Label.frame), Cgrectgetminy (Label.frame), Cgrectgetwidth (Label.frame),        Cgrectgetheight (Label.frame) +20);            } if ([Subviwe Iskindofclass:[uibutton class]]) {UIButton *button = (uibutton*) subviwe; if ([Button.titleLabel.text isequaltostring:@ "OK"]) {[button Settitlecolor:[uicolor Redcolor] Forstate:uic            Ontrolstatenormal];            } else {[button Settitlecolor:[wtdevice Getgreencolor] forstate:uicontrolstatenormal];        } Button.titleLabel.font = [Uifont systemfontofsize:18]; }    }}

The above code (the proxy section), in iOS7 and the following version is valid, but in iOS8 does not work, because IOS8 abandoned Uiactionsheet and Uialertview, instead of Uialertcontroller, the use of the following methods ( Instead of Uialertview):

#ifdef __IPHONE_8_0 if (target_is_ios8) {Uialertcontroller *actionsheetcontroller = [Uialertcontroller alertcontrollerwithtitle:@ "hint" m                                                                                    essage:@ "need to set allow access to the camera, see" Settings "," Help Center "            Preferredstyle:uialertcontrollerstylealert];                                                                   Uialertaction *actioncancel = [uialertaction actionwithtitle:@ "OK" Style:uialertactionstyledestructive handler:^ (U                        Ialertaction * action) {}];            [Actionsheetcontroller Addaction:actioncancel];            [Actionsheetcontroller.view Settintcolor:[wtdevice Getgreencolor];        [Self Presentviewcontroller:actionsheetcontroller animated:yes completion:nil]; } #endif if (Target_not_ios8) {UialerTView *alert = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ "requires settings to allow access to the camera, see" Settings "," Help Center "delegate:self            cancelbuttontitle:@ "Cancel" otherbuttontitles:nil];        [Alert show]; }

Instead of Uiactionsheet:

#ifdef __IPHONE_8_0 if (target_is_ios8) {Uialertcontroller *actionsheetcontroller = [Uialertcontroller alertCon trollerwithtitle:@ "Action option" message : Nil Preferredstyle:uialertcontrollerstylea        Ctionsheet]; Uialertaction *action0 = [uialertaction actionwithtitle:@ "option One" sty                                                           Le:uialertactionstyledefault handler:^ (Uialertaction * action) {                                                       [Self customMethod1];        }];                [Actionsheetcontroller Addaction:action0]; Uialertaction *action = [uialertaction actionwithtitle:@ "option two" Styl                     E:uialertactionstyledefault                                  handler:^ (Uialertaction * action) { [Self <span style= "font-family:arial, Helvetica, Sans-serif;"                                                       >customMethod2</span>];        }]; Uialertaction *action1 = [uialertaction actionwithtitle:@ "option three" St                                                            Yle:uialertactionstyledefault handler:^ (Uialertaction * action) {                                                        [Self customMethod3];        }];                                                               Uialertaction *actioncancel = [uialertaction actionwithtitle:@ "Cancel" Style:uialertactionstylecancel handler:^ (uialertaction                * action) {}];    [Actionsheetcontroller addaction:action];    [Actionsheetcontroller Addaction:action1];        [Actionsheetcontroller Addaction:actioncancel];        [Actionsheetcontroller.view Settintcolor:[uicolor Greencolor];    [Self Presentviewcontroller:actionsheetcontroller animated:yes completion:nil]; } #endif if (Target_not_ios8) {Uiactionsheet *as = [[Uiactionsheet alloc] initwithtitle:@ "Action Options" Delegate:sel        F cancelbuttontitle:@ "Cancel" destructivebuttontitle:nil otherbuttontitles:@ "option One", @ "option two", @ "option three", Nil];    [As ShowInView:self.view]; }

As for the difference between the two, it can be seen that iOS8 before the controller's view and covered a layer of view,ios8 after the present a controller and replaced the agent block, the code appears more compact.

The Uialertcontroller of the iOS8 of iOS8 development

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.