天天看點

iOS8開發之iOS8的UIAlertController

在ios8之前用uiactionsheet和uialertview來提供按鈕選擇和提示性資訊,比如uiactionsheet可以這樣寫:

iOS8開發之iOS8的UIAlertController

uiactionsheet *actionsheet = [[uiactionsheet alloc]    

                                 initwithtitle:@"title,nil時不顯示"    

                                 delegate:self    

                                 cancelbuttontitle:@"取消"    

                                 destructivebuttontitle:@"确定"    

                                 otherbuttontitles:@"第一項", @"第二項",nil];    

   actionsheet.actionsheetstyle = uiactionsheetstyleblackopaque;    

   [actionsheet showinview:self.view];  

然後在協定中實作代理:

iOS8開發之iOS8的UIAlertController

(void)actionsheet:(uiactionsheet *)actionsheet clickedbuttonatindex:(nsinteger)buttonindex    

{    

    if (buttonindex == 0) {    

        nslog(@"确定");    

    }else if (buttonindex == 1) {    

        nslog(@"第一項");    

    }else if(buttonindex == 2) {    

        nslog(@"第二項");    

    }else if(buttonindex == actionsheet.canclebuttonindex) {    

        nslog(@"取消");    

    }     

}    

- (void)actionsheetcancel:(uiactionsheet *)actionsheet{      

}      

-(void)actionsheet:(uiactionsheet *)actionsheet diddismisswithbuttonindex:(nsinteger)buttonindex{      

-(void)actionsheet:(uiactionsheet *)actionsheet willdismisswithbuttonindex:(nsinteger)buttonindex{      

如果需要修改按鈕字型、顔色等可以實作以下代理:

iOS8開發之iOS8的UIAlertController

- (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:@"确定"]) {  

                [button settitlecolor:[uicolor redcolor] forstate:uicontrolstatenormal];  

            } else {  

                [button settitlecolor:[wtdevice getgreencolor] forstate:uicontrolstatenormal];  

            }  

            button.titlelabel.font = [uifont systemfontofsize:18];  

    }  

}  

以上代碼(代理部分),在ios7及以下版本中是有效的,但是在ios8中卻不起作用,因為ios8抛棄了uiactionsheet和uialertview,取而代之的是uialertcontroller,其使用方法如下(代替uialertview):

iOS8開發之iOS8的UIAlertController

#ifdef __iphone_8_0  

        if (target_is_ios8) {  

            uialertcontroller *actionsheetcontroller = [uialertcontroller alertcontrollerwithtitle:@"提示"  

                                                                                           message:@"需要設定允許通路相機,操作方法見“設定”->“幫助中心”"  

                                                                                    preferredstyle:uialertcontrollerstylealert];  

            uialertaction *actioncancel = [uialertaction actionwithtitle:@"确定"  

                                                                   style:uialertactionstyledestructive  

                                                                 handler:^(uialertaction * 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:@"提示" message:@"需要設定允許通路相機,操作方法見“設定”->“幫助中心”" delegate:self cancelbuttontitle:@"取消" otherbuttontitles:nil];  

            [alert show];  

代替uiactionsheet:

iOS8開發之iOS8的UIAlertController

    if (target_is_ios8) {  

        uialertcontroller *actionsheetcontroller = [uialertcontroller alertcontrollerwithtitle:@"action選項"  

                                                                                       message:nil  

                                                                                preferredstyle:uialertcontrollerstyleactionsheet];  

        uialertaction *action0 = [uialertaction actionwithtitle:@"選項一"  

                                                         style:uialertactionstyledefault  

                                                       handler:^(uialertaction * action) {  

                                                           [self custommethod1];  

                                                       }];  

        [actionsheetcontroller addaction:action0];  

        uialertaction *action = [uialertaction actionwithtitle:@"選項二"  

                                                           [self <span style="font-family: arial, helvetica, sans-serif;">custommethod2</span>];  

        uialertaction *action1 = [uialertaction actionwithtitle:@"選項三"  

                                                          style:uialertactionstyledefault  

                                                        handler:^(uialertaction * action) {  

                                                            [self custommethod3];  

                                                        }];  

        uialertaction *actioncancel = [uialertaction actionwithtitle:@"取消"  

                                                               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];  

    if (target_not_ios8) {  

        uiactionsheet *as = [[uiactionsheet alloc] initwithtitle:@"action選項" delegate:self cancelbuttontitle:@"取消" destructivebuttontitle:nil otherbuttontitles:@"選項一",@"選項二",@"選項三", nil nil];  

        [as showinview:self.view];  

至于兩者的差別,可以看到,ios8之前是在controller的view上邊又覆寫了一層view,ios8之後則是present了一個controller并且将代理換成了block,代碼顯得更加緊湊。