UIAlertController-- iOS 8.0以后可用
.h文件
@interface PPAlertController : UIAlertController
@property (nonatomic, strong)UIColor * titleColor;
@property (nonatomic, strong)UIColor * cancelColor;
@property (nonatomic, strong)UIColor * sureColor;
@property (nonatomic, strong)UIAlertAction * cancelAction;
@property (nonatomic, strong)UIAlertAction * sureAction;
-(void)addCancelAction;
-(void)addSureActionWithBolck:(void(^)())block;
@end
.m文件
@implementation PPAlertController
-(void)setTitleColor:(UIColor *)titleColor{
NSMutableAttributedString * attStr = [[NSMutableAttributedString alloc] initWithString:self.title];
[attStr addAttributes:@{NSForegroundColorAttributeName:titleColor} range:[self.title rangeOfString:self.title options:NSBackwardsSearch]];
[self setValue:attStr forKey:@"attributedTitle"];
}
-(void)setCancelColor:(UIColor *)cancelColor{
if (self.cancelAction == nil) {
self.cancelAction = [UIAlertAction actionWithTitle:@"" style:UIAlertActionStyleCancel handler:nil];
}
[self.cancelAction setValue:cancelColor forKey:@"_titleTextColor"];
}
-(void)setSureColor:(UIColor *)sureColor{
if (self.sureAction != nil) {
[self.sureAction setValue:sureColor forKey:@"_titleTextColor"];
}
}
-(void)addCancelAction{
if (self.cancelAction==nil) {
self.cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
self.cancelColor = UIColorFromRGB(0xFF6699);
[self addAction:self.cancelAction];
}
}
-(void)addSureActionWithBolck:(void(^)())block{
if (self.sureAction==nil) {
self.sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
block();
}];
self.sureColor = [UIColor darkGrayColor];
[self addAction:self.sureAction];
}
}
@end
使用:
先引入头文件:
#import "PPAlertController.h"
初始化:
self.alertVC = [PPAlertController alertControllerWithTitle:@"T his is the title" message:@"This is the message" preferredStyle:UIAlertControllerStyleAlert];
self.alertVC.titleColor = [UIColor darkGrayColor];
[self.alertVC addCancelAction];
[self.alertVC addSureActionWithBolck:^{
//确定按钮点击后要处理的事件放在这里。
}];
根据需要灵活应用。
本人是菜鸟级别程序媛,请大家多多指教