iOS8開發之iOS8的UIAlertController

來源:互聯網
上載者:User

iOS8開發之iOS8的UIAlertController

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

 UIActionSheet *actionSheet = [[UIActionSheet alloc]                                    initWithTitle:@"title,nil時不顯示"                                    delegate:self                                    cancelButtonTitle:@"取消"                                    destructiveButtonTitle:@"確定"                                    otherButtonTitles:@"第一項", @"第二項",nil];      actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;      [actionSheet showInView:self.view];

然後在協議中實現代理:

(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{      }  

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

- (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):

#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:

#ifdef __IPHONE_8_0    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:@"選項二"                                                         style:UIAlertActionStyleDefault                                                       handler:^(UIAlertAction * action) {                                                           [self customMethod2];                                                       }];        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];    }#endif    if (TARGET_NOT_IOS8) {        UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"action選項" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"選項一",@"選項二",@"選項三", nil];        [as showInView:self.view];    }

至於兩者的區別,可以看到,iOS8之前是在controller的view上邊又覆蓋了一層view,iOS8之後則是present了一個controller並且將代理換成了block,代碼顯得更加緊湊。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.