发现一个有趣的,打开这个DEMO的时候,偶然发现这个DEMO还是自己在2016年的时候写的。如今还在继续使用,不得不说,那种由代码产生的喜悦 感,一下涌上心头。
简单的系统alertViewController的封装。原有的基础上增加了新API。有需要高级效果的,这个文章可以略过了。有兴趣可点个赞。
虽然东西比较简单,还是整理下吧。废话不多说,直接上代码。
.h
//
// KJCommonUI.h
// KJFrameworkProject
//
// Created by 王振 DemoKing on 2016/11/15.
// Copyright © 2016年 DemoKing. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
/// KJCommonUI
@interface KJCommonUI : NSObject
#pragma mark -
#pragma mark - 关于系统弹框
+ (KJCommonUI *)sharedManager;
- (void)hiddenMyAlertController;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 确定
*/
+ (UIAlertController *)showAlertViewWithSureMessage:(NSString *)message;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 OK
*/
+ (UIAlertController *)showAlertViewWithOKMessage:(NSString *)message;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 知道了
*/
+ (UIAlertController *)showAlertViewWithKnowMessage:(NSString *)message;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 确定
*/
+ (UIAlertController *)showAlertViewWithSureTitle:(NSString *)title;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 OK
*/
+ (UIAlertController *)showAlertViewWithOKTitle:(NSString *)title;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 知道了
*/
+ (UIAlertController *)showAlertViewWithKnowTitle:(NSString *)title;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语
@param cancelButtonTitle 取消按钮
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语
@param cancelButtonTitle 取消按钮
*/
+ (UIAlertController *)showAlertViewWithMessage:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithMessage:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 标题
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
弹框 UIAlertViewController 取消 + 确定 alert
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param sureButtonTitle 确定按钮
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
sureButtonTitle:(NSString *)sureButtonTitle
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void(^)(void))sureBlock;
/**
弹框 UIAlertViewController sheet
@param titleArray 按钮标题数组
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
弹框 UIAlertViewController sheet
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithCancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
弹框 UIAlertViewController sheet
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param viewController 视图控制器
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
inViewController:(id)viewController
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
弹框 UIAlertViewController sheet
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
弹框 UIAlertViewController 取消 + 确定 alert
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param sureButtonTitle 确定按钮
@param placeholder placeholder
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSingleTextFieldAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
sureButtonTitle:(NSString *)sureButtonTitle
placeholder:(NSString *)placeholder
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void(^)(UITextField *textFeild))sureBlock;
@end
.m
//
// KJCommonUI.m
// KJFrameworkProject
//
// Created by DEMOKING on 2016/11/15.
// Copyright © 2016年 DEMOKING. All rights reserved.
//
#import "KJCommonUI.h"
@interface KJCommonUI ()
@property (strong, nonatomic) UIAlertController *controller;
@end
@implementation KJCommonUI
+ (KJCommonUI *)sharedManager {
static KJCommonUI *sharedManager = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
sharedManager = [[KJCommonUI alloc] init];
});
return sharedManager;
}
- (void)hiddenMyAlertController {
if (self.controller) {
[self.controller dismissViewControllerAnimated:NO completion:nil];
}
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 确定
*/
+ (UIAlertController *)showAlertViewWithSureMessage:(NSString *)message {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:nil
message:message
cancelButtonTitle:@"确定"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 OK
*/
+ (UIAlertController *)showAlertViewWithOKMessage:(NSString *)message {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:nil
message:message
cancelButtonTitle:@"OK"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语 知道了
*/
+ (UIAlertController *)showAlertViewWithKnowMessage:(NSString *)message {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:nil
message:message
cancelButtonTitle:@"知道了"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 确定
*/
+ (UIAlertController *)showAlertViewWithSureTitle:(NSString *)title {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:nil
cancelButtonTitle:@"确定"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 OK
*/
+ (UIAlertController *)showAlertViewWithOKTitle:(NSString *)title {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:nil
cancelButtonTitle:@"OK"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语 知道了
*/
+ (UIAlertController *)showAlertViewWithKnowTitle:(NSString *)title {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:nil
cancelButtonTitle:@"知道了"
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 提示语
@param cancelButtonTitle 取消按钮
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:nil
cancelButtonTitle:cancelButtonTitle
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语
@param cancelButtonTitle 取消按钮
*/
+ (UIAlertController *)showAlertViewWithMessage:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:nil
message:message
cancelButtonTitle:cancelButtonTitle
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param message 提示语
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithMessage:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:nil
message:message
cancelButtonTitle:cancelButtonTitle
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:cancelBlock
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 标题
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:nil
cancelButtonTitle:cancelButtonTitle
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:cancelBlock
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert window弹出
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param cancelBlock 取消回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *viewController = [KJCommonUI showAlertViewWithTitle:title
message:message
cancelButtonTitle:cancelButtonTitle
sureButtonTitle:nil
inViewController:window.rootViewController
cancelBlock:cancelBlock
sureBlock:nil];
return viewController;
}
/**
弹框 UIAlertViewController
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param sureButtonTitle 确定按钮
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
sureButtonTitle:(NSString *)sureButtonTitle
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void(^)(void))sureBlock {
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
if (sureButtonTitle) {
UIAlertAction * actionSure = [UIAlertAction actionWithTitle:sureButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[KJCommonUI sharedManager].controller = nil;
if (sureBlock) {
sureBlock();
}
}];
[alertVC addAction:actionSure];
}
if (cancelButtonTitle.length) {
UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[KJCommonUI sharedManager].controller = nil;
if (cancelBlock) {
cancelBlock();
}
}];
[alertVC addAction:actionCancel];
}
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPad"]) {
alertVC.popoverPresentationController.sourceView = [(UIViewController *)viewController view];
}
[viewController presentViewController:alertVC animated:YES completion:nil];
[KJCommonUI sharedManager].controller = alertVC;
return alertVC;
}
/**
弹框 UIAlertViewController sheet
@param titleArray 按钮标题数组
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *alertViewController = [KJCommonUI showSheetViewWithTitle:nil
message:nil
cancelButtonTitle:@"取消"
titleArray:titleArray
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:sureBlock];
return alertViewController;
}
/**
弹框 UIAlertViewController sheet
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithCancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
UIAlertController *alertViewController = [KJCommonUI showSheetViewWithTitle:nil
message:nil
cancelButtonTitle:cancelButtonTitle
titleArray:titleArray
inViewController:window.rootViewController
cancelBlock:nil
sureBlock:sureBlock];
return alertViewController;
}
/**
弹框 UIAlertViewController sheet
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param viewController 视图控制器
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
inViewController:(id)viewController
sureBlock:(void(^)(UIAlertAction *action))sureBlock {
UIAlertController *alertViewController = [KJCommonUI showSheetViewWithTitle:title
message:message
cancelButtonTitle:cancelButtonTitle
titleArray:titleArray
inViewController:viewController
cancelBlock:nil
sureBlock:sureBlock];
return alertViewController;
}
/**
弹框 UIAlertViewController sheet
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param titleArray 按钮标题数组
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSheetViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void (^)(UIAlertAction *))sureBlock {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
[titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
UIAlertAction * actionSure = [UIAlertAction actionWithTitle:obj style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[KJCommonUI sharedManager].controller = nil;
if (sureBlock) {
sureBlock(action);
}
}];
[alertVC addAction:actionSure];
}];
if (cancelButtonTitle) {
UIAlertAction * actionCancel = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[KJCommonUI sharedManager].controller = nil;
if (cancelBlock) {
cancelBlock();
}
}];
[alertVC addAction:actionCancel];
}
NSString *deviceType = [UIDevice currentDevice].model;
if([deviceType isEqualToString:@"iPad"]) {
UIPopoverPresentationController * popPresenter = [alertVC popoverPresentationController];
popPresenter.sourceView = [viewController view];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
popPresenter.sourceRect = CGRectMake(width / 2 - 152, height, 304, 127.5);
}
[viewController presentViewController:alertVC animated:YES completion:nil];
[KJCommonUI sharedManager].controller = alertVC;
return alertVC;
}
/**
弹框 UIAlertViewController 取消 + 确定 alert
@param title 标题
@param message 提示语
@param cancelButtonTitle 取消按钮
@param sureButtonTitle 确定按钮
@param placeholder placeholder
@param viewController 视图控制器
@param cancelBlock 取消回调
@param sureBlock 确定回调
*/
+ (UIAlertController *)showSingleTextFieldAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
sureButtonTitle:(NSString *)sureButtonTitle
placeholder:(NSString *)placeholder
inViewController:(id)viewController
cancelBlock:(void(^)(void))cancelBlock
sureBlock:(void(^)(UITextField *textFeild))sureBlock {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:title
message:message
preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.returnKeyType = UIReturnKeyDone;
textField.placeholder = placeholder;
textField.delegate = viewController;
}];
UIAlertAction* okAction = [UIAlertAction actionWithTitle:sureButtonTitle style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
UITextField *text = [alert.textFields firstObject];
[KJCommonUI sharedManager].controller = nil;
if (sureBlock) {
sureBlock(text);
}
}];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
[KJCommonUI sharedManager].controller = nil;
if (cancelBlock) {
cancelBlock();
}
}];
[alert addAction:okAction];
[alert addAction:cancelAction];
[viewController presentViewController:alert animated:YES completion:nil];
[KJCommonUI sharedManager].controller = alert;
return alert;
}
@end
注意事项,一起来思考几个问题:
1、适配ipad的问题;
2、window的获取问题;
3、是否需要单例类持有的问题;
V动动手指(woai1101992)点个赞吧X
IMG_0319.JPG
网友评论