美文网首页
UIAlertController简单使用

UIAlertController简单使用

作者: Fsn_soul | 来源:发表于2016-06-10 16:19 被阅读103次

    UIAlertController是iOS8新出的API,它主要是替代以前的UIAlertView和UIActionSheet.UIAlertController实现的功能主要是弹窗,和从底部弹出菜单.
    使用如下:

    弹窗
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您的余额已不足,请及时充值!" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"------");
        }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"+++++++");
        }];
    //    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
    //        NSLog(@"========");
    //    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    //    [alertController addAction:resetAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
    弹菜单
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无间道" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:@"无间道"];
        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, attrStr.string.length)];
        [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, attrStr.string.length)];
        [alertController setValue:attrStr forKey:@"attributedTitle"];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"---取消---");
        }];
        [cancelAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
    
        UIAlertAction *takePhotoAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"---拍照---");
        }];
        [takePhotoAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
        
        UIAlertAction *selectPhotoAction = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSLog(@"+++从手机相册选择++++");
        }];
        [selectPhotoAction setValue:[UIColor blackColor] forKey:@"titleTextColor"];
       
        [alertController addAction:cancelAction];
        [alertController addAction:takePhotoAction];
        [alertController addAction:selectPhotoAction];
        [self presentViewController:alertController animated:YES completion:nil];
    

    考虑到系统的UIActionSheet在iOS8已经被废弃了,而UIAlertController在自定义颜色,字体方面不是很方便(从上面就可以看出了),而且UIAlertController需要iOS8及以上系统,但是现在还需要适配iOS7.所以我自己做了一个用来弹出菜单的控件XQActionSheet,XQActionSheet可以很方便的自定义"菜单"上的文字颜色,文字字体.并且采用block回调的方式配置按钮及执行按钮事件,让代码更加紧凑.

    XQActionSheet使用

        XQActionSheet *sheet = [XQActionSheet actionSheetControllerWithTitle:@"温馨提示" message:@"您的余额已不足,请及时充值!"];
        sheet.sheetTitleLabel.textColor = [UIColor greenColor];
        sheet.sheetMessageLabel.textColor = [UIColor redColor];
        [sheet.cancelButton setTitleColor:[UIColor brownColor] forState:UIControlStateNormal];
        [sheet addBtnWithTitle:@"充值" configHandler:^(UIButton *button) {
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        } actionHandler:^(UIButton *button) {
            NSLog(@"充值");
        }];
        [sheet addBtnWithTitle:@"充满" configHandler:^(UIButton *button) {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        } actionHandler:^(UIButton *button) {
            NSLog(@"充满");
        }];
        [sheet addBtnWithTitle:@"不要再提示我" configHandler:^(UIButton *button) {
            [button setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
        } actionHandler:^(UIButton *button) {
             NSLog(@"不要再提示我");
        }];
        [sheet addBtnWithTitle:@"听说标题有点长的长的都很帅" configHandler:nil actionHandler:^(UIButton *button) {
            NSLog(@"听说标题有点长的长的都很帅");
        }];
        [sheet showWithViewController:self];
    

    效果

    XQActionSheet@2x.png

    Github地址:

    https://github.com/xq-120/XQActionSheet
    喜欢的朋友,可以点点关注,送送赞奥.

    相关文章

      网友评论

          本文标题:UIAlertController简单使用

          本文链接:https://www.haomeiwen.com/subject/pqusdttx.html