UIAlertViewController
- UIAlertView and UIActionSheet are deprecated in iOS 9.0.
1.提示框 UIAlertControllerStyleAlert
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"];
2.选项栏 UIAlertControllerStyleActionSheet
#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
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);
}
}
}
附: 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样式,实现可替换背景和按钮