發(fā)現(xiàn)一個有趣的郎笆,打開這個DEMO的時候,偶然發(fā)現(xiàn)這個DEMO還是自己在2016年的時候?qū)懙募嗌簟H缃襁€在繼續(xù)使用旅赢,不得不說,那種由代碼產(chǎn)生的喜悅 感惑惶,一下涌上心頭煮盼。
簡單的系統(tǒng)alertViewController的封裝。原有的基礎(chǔ)上增加了新API带污。有需要高級效果的僵控,這個文章可以略過了。有興趣可點(diǎn)個贊刮刑。
雖然東西比較簡單喉祭,還是整理下吧养渴。廢話不多說,直接上代碼。
.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 - 關(guān)于系統(tǒng)彈框
+ (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 取消回調(diào)
*/
+ (UIAlertController *)showAlertViewWithMessage:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
彈框 UIAlertViewController 取消 + 確定 alert window彈出
@param title 標(biāo)題
@param cancelButtonTitle 取消按鈕
@param cancelBlock 取消回調(diào)
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
彈框 UIAlertViewController 取消 + 確定 alert window彈出
@param title 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param cancelBlock 取消回調(diào)
*/
+ (UIAlertController *)showAlertViewWithTitle:(NSString *)title
message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
cancelBlock:(void(^)(void))cancelBlock;
/**
彈框 UIAlertViewController 取消 + 確定 alert
@param title 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param sureButtonTitle 確定按鈕
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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 按鈕標(biāo)題數(shù)組
@param sureBlock 確定回調(diào)
*/
+ (UIAlertController *)showSheetViewWithTitleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
彈框 UIAlertViewController sheet
@param cancelButtonTitle 取消按鈕
@param titleArray 按鈕標(biāo)題數(shù)組
@param sureBlock 確定回調(diào)
*/
+ (UIAlertController *)showSheetViewWithCancelButtonTitle:(NSString *)cancelButtonTitle
titleArray:(NSArray <NSString *>*)titleArray
sureBlock:(void(^)(UIAlertAction *action))sureBlock;
/**
彈框 UIAlertViewController sheet
@param title 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param titleArray 按鈕標(biāo)題數(shù)組
@param viewController 視圖控制器
@param sureBlock 確定回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param titleArray 按鈕標(biāo)題數(shù)組
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param sureButtonTitle 確定按鈕
@param placeholder placeholder
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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 取消回調(diào)
*/
+ (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 標(biāo)題
@param cancelButtonTitle 取消按鈕
@param cancelBlock 取消回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param cancelBlock 取消回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param sureButtonTitle 確定按鈕
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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 按鈕標(biāo)題數(shù)組
@param sureBlock 確定回調(diào)
*/
+ (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 按鈕標(biāo)題數(shù)組
@param sureBlock 確定回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param titleArray 按鈕標(biāo)題數(shù)組
@param viewController 視圖控制器
@param sureBlock 確定回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param titleArray 按鈕標(biāo)題數(shù)組
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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 標(biāo)題
@param message 提示語
@param cancelButtonTitle 取消按鈕
@param sureButtonTitle 確定按鈕
@param placeholder placeholder
@param viewController 視圖控制器
@param cancelBlock 取消回調(diào)
@param sureBlock 確定回調(diào)
*/
+ (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
注意事項(xiàng)店量,一起來思考幾個問題:
1余佃、適配ipad的問題;
2藐唠、window的獲取問題帆疟;
3、是否需要單例類持有的問題宇立;
V動動手指(woai1101992)點(diǎn)個贊吧X