我们项目里面有好多用到了弹出视图来警告,提示之类的,写的多了,发现好烦,就写了一个工具
在. h 文件
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface AlertViewTool : NSObject
+ (void)showAlertViewWithTitle:(NSString *)alertTitle message:(NSString *)msg cancelBtnTitle:(NSString *)cancelBtnTitle;
@end
.m
#import "AlertViewTool.h"
@implementation AlertViewTool
+ (void)showAlertViewWithTitle:(NSString *)alertTitle message:(NSString *)msg cancelBtnTitle:(NSString *)cancelBtnTitle
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertAction actionWithTitle:cancelBtnTitle style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:action];
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}
@end
最后,当用到的时候,直接调用一下就行了
[AlertViewTool showAlertViewWithTitle:@"测试" message:@"正在测试" cancelBtnTitle:@"确定"];
搞定了.