iOS:便捷使用UIAlertController弹框
作者:互联网
UIAlertController弹框的使用虽然不复杂,但是代码有点多,如果项目中频繁使用UIAlertController弹框的话会显得代码很累赘。
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
// 弹出对话框
[object presentViewController:alert animated:true completion:nil];
下面创建一个类用来统一处理弹窗,将需要实现的方法与对象通过参数的形式传递进来。
HYAlertManager.h文件
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@interface HYAlertManager : NSObject
+(instancetype)shareManager;
-(void)alertTitle:(NSString *)title message:(NSString *)message commit:(SEL)commitMethod object:(id)object;
@end
HYAlertManager.m文件
//传递函数、参数
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
#import "HYAlertManager.h"
@implementation HYAlertManager
static HYAlertManager *alertManager = nil;
+(instancetype)shareManager{
@synchronized ([HYAlertManager class]) {
if (alertManager == nil) {
alertManager = [[HYAlertManager alloc] init];
}
}
return alertManager;
}
+(instancetype)alloc{
@synchronized ([HYAlertManager class]) {
alertManager = [super alloc];
}
return alertManager;
}
-(void)alertTitle:(NSString *)title message:(NSString *)message commit:(SEL)commitMethod object:(id)object{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
SuppressPerformSelectorLeakWarning([object performSelector:commitMethod withObject:nil]);
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
// 弹出对话框
[object presentViewController:alert animated:true completion:nil];
}
@end
调用
#import "ViewController.h"
#import "HYAlertManager.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel *label;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 100, 50, 30);
[btn setTitle:@"弹框" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor = [UIColor orangeColor];
[self.view addSubview:btn];
self.label = [[UILabel alloc] initWithFrame:CGRectMake(20, 60, 250, 30)];
[self.view addSubview:self.label];
}
-(void)btnClick{
[[HYAlertManager shareManager] alertTitle:@"提示" message:@"即将显示当前时间" commit:@selector(commit) object:self];
}
-(void)commit{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [NSDate new];
self.label.text = [formatter stringFromDate:date];
}
@end
小伙伴们可以根据自己的需要设置参数。
下面是为需要的小伙伴们提供的demo:https://github.com/ITHanYong/AlertManager.git
标签:btn,UIAlertAction,object,iOS,弹框,HYAlertManager,UIAlertController,message,alert 来源: https://blog.csdn.net/qq_15289761/article/details/106830696