键盘

来源:互联网 发布:手机画电路图软件 编辑:程序博客网 时间:2024/04/30 11:47

关于UIAlertView键盘弹起的问题

作者 晓飞76
2016.11.21 16:49 字数 630 阅读 175评论 0

1.问题:pop 一个viewController时候键盘会发生闪现

假如有两个ViewController A 和 B(使用了UINavigationController), 在B中的TextField操作结束后,使用UIAlertView提醒再返回到A界面,键盘会闪现出来,即使写了[_textField resignFirstResponder] 和 [self.view endEditing:YES]; 也还是会发生。

验证方法:在A和B控制器中都去调用textField的代理,这个时候,可以看到A和B中的代理都调用了。

解决方法:

  • 方法一:这个问题就是因为键盘收起是有动画的。而在键盘收起的动画开始的时候就pop了,键盘的动画没有执行完当然要在下一个vc里继续执行。所以要等键盘完全收起之后再pop或者push。直接dispatch_after个0.5秒左右再执行pop或者push。至于为什么用0.5秒,可能因为系统键盘收起的duration在0.5内会执行完毕.
//或者等键盘动画结束后再弹出AlertViewdispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{         [alert show];});
  • 方法二:添加UITextFieldDelegate,并使alert调用出来的textField的delegate = self;最后在alert的点击事件处添加
    [alertView textFieldAtIndex:buttonIndex]]resignFirstResponder];

  • 方法三:由原因是alertview关闭影响了系统其他的动画导致的。要么延迟调用,或者自定义一个alertview。

2.跟适配有关的设置

iOS8之后,有了UIAlertController这个类,如下

NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController

很明显,苹果强烈建议我们如果能不用UIAlertView就不要用啦,因为我们有UIAlertController了!


为了兼容iOS7,我们的项目中就统一使用了UIAlertView。问题来了:(项目中的某一)界面中textField处于编辑状态(界面上有键盘),点击界面中一个执行确定操作的按钮时,我先将键盘收起随即又执行了弹出一个UIAlertView的代码,点击UIAlertView上的确定按钮之后其被dismiss掉了。这时,键盘又神奇般的弹了出来

兼容代码:

if (IOS_SystemVersion >= 8.0)  {        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {        }];        [alertController addAction:okAction];        [self presentViewController:alertController animated:YES completion:nil];    }    else  {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];        [alert show];    }

有人说在iOS 8.3以后,dismiss alert view时系统会尝试恢复之前的keyboard input。可有一点我不明白的是,我明明执行了收起键盘的代码,不知道苹果是怎么处理的。

有人用NSTimer,0.25秒(键盘收起的动画时间)后,再去pop,像下面这样

self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(popToPreviousViewController) userInfo:nil repeats:NO];//或者[self performSelector:@selector(popVC) withObject:nil afterDelay:0.25];都能解决问题。

但我建议:兼容的方式

if (IOS_SystemVersion >= 8.0)  {        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"alert" message:@"l am alert" preferredStyle:UIAlertControllerStyleAlert];        UIAlertAction *action = [UIAlertAction actionWithTitle:@"l know" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {            [self.navigationController popViewControllerAnimated:YES];        }];        [alertController addAction:action];        [self presentViewController:alertController animated:YES completion:nil];    }    else {        [self.navigationController popViewControllerAnimated:YES];        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"alertView" message:@"l am alertView" delegate:nil cancelButtonTitle:@"pop" otherButtonTitles:nil];        [alertView show];    }


或者

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    
    if ([buttonTitle isEqualToString:@"客服"]) {
     
    } else if ([buttonTitle isEqualToString:@"确认"]) {
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; // 避免alertView点击弹出键盘
        [self stopBroadcast];
        
    }
}

本文参考:http://www.cnblogs.com/android-wuwei/p/4685960.html

原创粉丝点击