UIAlertController写起来比较繁琐, 这里进行一下封装方便使用
你只需要调用该方法即可:
[CommonMethod showAlertControllerTitle:nil msg:@"你真的要退出吗?" superVC:self preferredStyle:(UIAlertControllerStyleAlert) okCallBack:^{
NSLog(@"点击确定回调");
} cancleCallBack:^{
NSLog(@"点击取消 回调");
}];
首先建立一个CommMethod 继承NSObject
.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CommonMethod : NSObject
+ (void)showAlertControllerTitle:(NSString *)title msg:(NSString *)msg superVC:(UIViewController *)superVC preferredStyle:(UIAlertControllerStyle)preferredStyle okCallBack:(void(^)(void))okCallBack cancleCallBack:(void(^)(void))cancleCallBack;
@end
.m
#import "CommonMethod.h"
#import <UIKit/UIKit.h>
@implementation CommonMethod
+ (void)showAlertControllerTitle:(NSString *)title msg:(NSString *)msg superVC:(UIViewController *)superVC preferredStyle:(UIAlertControllerStyle)preferredStyle okCallBack:(void(^)(void))okCallBack cancleCallBack:(void(^)(void))cancleCallBack {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:preferredStyle];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (okCallBack) {
okCallBack();
}
}];
UIAlertAction *canAction = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
if (cancleCallBack) {
cancleCallBack();
}
}];
[alertController addAction:okAction];
[alertController addAction:canAction];
[superVC presentViewController:alertController animated:YES completion:nil];
}
@end
在Controller中使用
[CommonMethod showAlertControllerTitle:nil msg:@"你真的要退出吗?" superVC:self preferredStyle:(UIAlertControllerStyleAlert) okCallBack:^{
NSLog(@"点击确定回调");
} cancleCallBack:^{
NSLog(@"点击取消 回调");
}];