UIAlertController

UIAlertViewController

  • UIAlertView and UIActionSheet are deprecated in iOS 9.0.

1.提示框 UIAlertControllerStyleAlert

UIAlertController.png
NSString *title = NSLocalizedString(@"Title", nil);
NSString *message = NSLocalizedString(@"I'm message", nil);
NSString *cancelTitle = NSLocalizedString(@"Cancel", nil);
NSString *okTitle = NSLocalizedString(@"OK", nil);

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    }];
    
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:okTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSURL *urlStr = [NSURL URLWithString:@"https://appsto.re/cn/JZj6bb.i"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if ([[UIApplication sharedApplication] canOpenURL:urlStr]) {
                [[UIApplication sharedApplication] openURL:urlStr];
            }
        });
    }];
   // 添加取消按钮才能点击空白隐藏
    [alertController addAction:cancelAction];
    [alertController addAction:OKAction];
    
    [self.window.rootViewController presentViewController:alertController animated:YES completion:nil];

修改title 和Message属性

// title
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:@"Tip"];
[alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:20] range:NSMakeRange(0, 3)];
[alertController setValue:alertControllerStr forKey:@"attributedTitle"];
    
// message
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:@"Message Content"];
[alertControllerMessageStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(4, 7)];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, 15)];
[alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
UIAlertController.png

2.选项栏 UIAlertControllerStyleActionSheet

Sheet.png
#define videoActionText NSLocalizedString(@"Video", nil)
#define gifActionText   NSLocalizedString(@"Gif", nil)
#define photoActionText NSLocalizedString(@"Take Photos", nil)
#define albumActionText NSLocalizedString(@"Album", nil)
#define cancelText      NSLocalizedString(@"Cancle", nil)

UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *videoAction = [UIAlertAction actionWithTitle:videoActionText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [self showCustomCamera];
    }];
    UIAlertAction *gifAction = [UIAlertAction actionWithTitle:gifActionText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:photoActionText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *albumAction = [UIAlertAction actionWithTitle:albumActionText style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelText style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    // KVC 改变颜色
    [cancelAction setValue:[UIColor redColor] forKey:@"_titleTextColor"];

    [alert addAction:videoAction];
    [alert addAction:gifAction];
    [alert addAction:photoAction];
    [alert addAction:albumAction];
    [alert addAction:cancelAction];

    [self presentViewController:alert animated:YES completion:nil];

安全改变

unsigned int count = 0;
objc_property_t *propertys = class_copyPropertyList([UIAlertAction class], &count);
for(int i = 0; i < count; i ++) {
        objc_property_t property = propertys[i];
        NSString *propertyName = [NSString stringWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        if ([propertyName isEqualToString:@"_titleTextColor"]) {
                [self setValue:[UIColor whiteColor] forKey:propertyName];
            }
    }            

提示框上加UIDatepicker

AlertDatePicker.png
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIDatePicker *datePicker=[[UIDatePicker alloc] init];
    datePicker.datePickerMode=UIDatePickerModeDate;
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];// 设置日期选择控件的地区
    datePicker.locale = locale;
    [alertVC.view addSubview:datePicker];
    datePicker.ddy_w = DDYSCREENW;
    datePicker.ddy_x = alertVC.view.ddy_x;
    datePicker.ddy_y = alertVC.view.ddy_y + 10;
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
    [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
    [alertVC addAction:cancelAction];
    [self presentViewController:alertVC animated:YES completion:nil];

Alert倒计时

#pragma mark 收到邀请连麦请求
- (void)receiveLinkInvite:(NSDictionary *)dict
{
    __weak __typeof__ (self)weakSelf = self;
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示"
                                                                     message:[NSString stringWithFormat:@"%@向您发起连麦请求", dict[@"username"]]
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        [weakSelf.msgHandler refusedLink:[dict[@"userid"] asNSString]];
    }];
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"同意" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        _sessionId = dict[@"sessionid"];
        [weakSelf getUploadLinkURL];
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:OKAction];
    [self presentViewController:alertVC animated:YES completion:nil];
    
    if (!_timer) {
        // 倒计时时间
        __block int timeout = 60;
        if (timeout!=0) {
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if (timeout<=0) {
                    dispatch_source_cancel(_timer);
                    _timer = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [OKAction setValue:[NSString stringWithFormat:@"%@(%02d)", @"同意", 0] forKey:@"title"];
                        [alertVC  dismissViewControllerAnimated:YES completion:^{ [weakSelf.msgHandler refusedLink:[dict[@"userid"] asNSString]]; }];
                    });
                } else {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [OKAction setValue:[NSString stringWithFormat:@"%@(%02d)", @"同意",timeout] forKey:@"title"];
                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }
    }
}
Alert.png

附: Runtime

/////////////////////////////////////
UIAlertController
uialertion.ivarName = _message
uialertion.ivarName = _attributedTitle
uialertion.ivarName = _attributedMessage
uialertion.ivarName = _attributedDetailMessage
uialertion.ivarName = _actionDelimiterIndices
uialertion.ivarName = _linkedAlertControllers
uialertion.ivarName = _cancelAction
uialertion.ivarName = _actionToKeyCommandsDictionary
uialertion.ivarName = _keyCommandToActionMapTable
uialertion.ivarName = _resolvedStyle
uialertion.ivarName = _preferredStyle
uialertion.ivarName = _styleProvider
uialertion.ivarName = _contentViewController
uialertion.ivarName = _textFieldViewController
uialertion.ivarName = _backButtonDismissGestureRecognizer
uialertion.ivarName = _selectGestureRecognizer
uialertion.ivarName = _ownedTransitioningDelegate
uialertion.ivarName = _shouldInformViewOfAddedContentViewController
uialertion.ivarName = _isInSupportedInterfaceOrientations
uialertion.ivarName = _shouldEnsureContentControllerViewIsVisibleOnAppearance
uialertion.ivarName = _hidden
uialertion.ivarName = _ignoresKeyboardForPositioning
uialertion.ivarName = __shouldAllowNilParameters
uialertion.ivarName = _hasPreservedInputViews
uialertion.ivarName = __shouldFlipFrameForShimDismissal
uialertion.ivarName = _actions
uialertion.ivarName = _preferredAction
uialertion.ivarName = __presentationSourceRepresentationView
uialertion.ivarName = __visualStyle
uialertion.ivarName = __compatibilityPopoverController
uialertion.ivarName = __systemProvidedPresentationView
uialertion.ivarName = __systemProvidedPresentationDelegate
uialertion.ivarName = _systemProvidedGestureRecognizer
uialertion.ivarName = _temporaryAnimationCoordinator
uialertion.ivarName = _previewInteractionController

uialertion.property = actions
uialertion.property = preferredAction
uialertion.property = textFields
uialertion.property = title
uialertion.property = message
uialertion.property = preferredStyle
uialertion.property = attributedTitle
uialertion.property = attributedMessage
uialertion.property = contentViewController
uialertion.property = styleProvider
uialertion.property = _defaultAlertAction
uialertion.property = _shouldEnsureContentControllerViewIsVisibleOnAppearance
uialertion.property = _attributedDetailMessage
uialertion.property = ignoresKeyboardForPositioning
uialertion.property = _presentationSourceRepresentationView
uialertion.property = _shouldReverseActions
uialertion.property = _visualStyle
uialertion.property = _actions
uialertion.property = _cancelAction
uialertion.property = _focusedAction
uialertion.property = _actionDelimiterIndices
uialertion.property = _foregroundView
uialertion.property = _dimmingView
uialertion.property = _resolvedStyle
uialertion.property = _shouldProvideDimmingView
uialertion.property = _shouldAlignToKeyboard
uialertion.property = _hidden
uialertion.property = effectAlpha
uialertion.property = _shouldAllowNilParameters
uialertion.property = _compatibilityPopoverController
uialertion.property = _textFieldViewController
uialertion.property = _textFieldsHidden
uialertion.property = hasPreservedInputViews
uialertion.property = _systemProvidedPresentationView
uialertion.property = _systemProvidedPresentationDelegate
uialertion.property = systemProvidedGestureRecognizer
uialertion.property = _shouldFlipFrameForShimDismissal
uialertion.property = temporaryAnimationCoordinator
uialertion.property = previewInteractionController
uialertion.property = hash
uialertion.property = superclass
uialertion.property = description
uialertion.property = debugDescription

/////////////////////////////////////
UIActionSheet
uialertion.ivarName = _alertController
uialertion.ivarName = _presenter
uialertion.ivarName = _retainedSelf
uialertion.ivarName = _actions
uialertion.ivarName = _cancelIndex
uialertion.ivarName = _firstOtherButtonIndex
uialertion.ivarName = _destructiveButtonIndex
uialertion.ivarName = _actionSheetStyle
uialertion.ivarName = _context
uialertion.ivarName = _hasPreparedAlertActions
uialertion.ivarName = _isPresented
uialertion.ivarName = _alertControllerShouldDismiss
uialertion.ivarName = _handlingAlertActionShouldDismiss
uialertion.ivarName = _dismissingAlertController
uialertion.ivarName = _delegate

uialertion.property = delegate
uialertion.property = title
uialertion.property = actionSheetStyle
uialertion.property = numberOfButtons
uialertion.property = cancelButtonIndex
uialertion.property = destructiveButtonIndex
uialertion.property = firstOtherButtonIndex
uialertion.property = visible
uialertion.property = hash
uialertion.property = superclass
uialertion.property = description
uialertion.property = debugDescription

/////////////////////////////////////
UIAlertAction
uialertion.ivarName = _title
uialertion.ivarName = _titleTextAlignment
uialertion.ivarName = _enabled
uialertion.ivarName = _checked
uialertion.ivarName = _isPreferred
uialertion.ivarName = _imageTintColor
uialertion.ivarName = _titleTextColor
uialertion.ivarName = _style
uialertion.ivarName = _handler
uialertion.ivarName = _simpleHandler
uialertion.ivarName = _image
uialertion.ivarName = _shouldDismissHandler
uialertion.ivarName = __descriptiveText
uialertion.ivarName = _contentViewController
uialertion.ivarName = _keyCommandInput
uialertion.ivarName = _keyCommandModifierFlags
uialertion.ivarName = __representer
uialertion.ivarName = __alertController

uialertion.property = title
uialertion.property = style
uialertion.property = enabled
uialertion.property = handler
uialertion.property = simpleHandler
uialertion.property = image
uialertion.property = shouldDismissHandler
uialertion.property = _descriptiveText
uialertion.property = contentViewController
uialertion.property = keyCommandInput
uialertion.property = keyCommandModifierFlags
uialertion.property = _representer
uialertion.property = _isPreferred
uialertion.property = _alertController

推荐

改变UIAlertView和UIActionSheet颜色
LGAlertView
MSAlertController
添加输入框
自定义UIAlertView样式,实现可替换背景和按钮

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,542评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,596评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,021评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,682评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,792评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,985评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,107评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,845评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,299评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,612评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,747评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,441评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,072评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,828评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,069评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,545评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,658评论 2 350

推荐阅读更多精彩内容