背景:在開發過程中,iOS8之后,當觸發textField時鍵盤響應,此時如果點擊按鈕彈出UIAlertView點擊取消或者確定時,鍵盤會收回再次彈出(閃現一次)
在iOS8之后,UIAlertView被棄用,改用UIAlertController,所以在使用過程中需要對系統進行判斷,在iOS8之前用UIAlertView,在iOS8之后用UIAlertController,在這里我做了一下封裝,直接上代碼:
//宏定義 判斷手機系統 #define iOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) #define iOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) #define iOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0) |
創建自定義的AlertView(我這里就直接用ZGYAlertView)繼承NSObject
.h文件中 typedef NS_ENUM(NSInteger){ CANCLEBTN = 0, ENTERBTN, }AlertViewBtnIndex; typedef void(^AlertViewSselectBlock)(AlertViewBtnIndex index); @interface ZGYAlertView : NSObject @property (nonatomic, copy) AlertViewSselectBlock block; - (void)showAlertViewMessage:(NSString *)msg Title:(NSString *)title cancleItem:(NSString *)cancle andOtherItem:(NSString *)other viewController:(UIViewController *)controller onBlock:(void (^)(AlertViewBtnIndex))alertViewBlock; |
.m文件中代碼 @interface ZGYAlertView (){ AlertViewSselectBlock _alertViewBlock; } @end @implementation ZGYAlertView - (void)showAlertViewMessage:(NSString *)msg Title:(NSString *)title cancleItem:(NSString *)cancle andOtherItem:(NSString *)other viewController:(UIViewController *)controller onBlock:(void (^)(AlertViewBtnIndex))alertViewBlock{ if (iOS8) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert]; if (cancle != nil) { [alert addAction:[UIAlertAction actionWithTitle:cancle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { alertViewBlock(CANCLEBTN); }]]; } if (other != nil) { [alert addAction:[UIAlertAction actionWithTitle:other style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { alertViewBlock(ENTERBTN); }]]; } [controller presentViewController:alert animated:YES completion:nil]; }else{ UIAlertView *goHomePage = [[UIAlertView alloc] initWithTitle:title message:msg delegate:controller cancelButtonTitle:cancle otherButtonTitles:other, nil]; [goHomePage show]; _alertViewBlock = alertViewBlock; } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ _alertViewBlock(buttonIndex); } |
完成之后直接調用即可,采用block回調的方式監聽點擊取消或者確定的按鈕。
手機瀏覽
全國
19974831731





