iOS 关于AlertView与键盘冲突解决

背景:在开发过程中,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回调的方式监听点击取消或者确定的按钮。

第一次写,各位大神勿喷,谢谢!欢迎纠错!

你可能感兴趣的:(iOS 关于AlertView与键盘冲突解决)