1.問題:pop 一個viewController時候鍵盤會發(fā)生閃現(xiàn)
假如有兩個ViewController A 和 B(使用了UINavigationController), 在B中的TextField操作結(jié)束后,使用UIAlertView提醒再返回到A界面,鍵盤會閃現(xiàn)出來,即使寫了[_textField resignFirstResponder] 和 [self.view endEditing:YES]; 也還是會發(fā)生。
驗證方法:在A和B控制器中都去調(diào)用textField的代理,這個時候,可以看到A和B中的代理都調(diào)用了。
解決方法:
- 方法一:這個問題就是因為鍵盤收起是有動畫的。而在鍵盤收起的動畫開始的時候就pop了,鍵盤的動畫沒有執(zhí)行完當(dāng)然要在下一個vc里繼續(xù)執(zhí)行。所以要等鍵盤完全收起之后再pop或者push。直接dispatch_after個0.5秒左右再執(zhí)行pop或者push。至于為什么用0.5秒,可能因為系統(tǒng)鍵盤收起的duration在0.5內(nèi)會執(zhí)行完畢.
//或者等鍵盤動畫結(jié)束后再彈出AlertView
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[alert show];
});
方法二:添加UITextFieldDelegate,并使alert調(diào)用出來的textField的delegate = self;最后在alert的點(diǎn)擊事件處添加
[alertView textFieldAtIndex:buttonIndex]]resignFirstResponder];方法三:由原因是alertview關(guān)閉影響了系統(tǒng)其他的動畫導(dǎo)致的。要么延遲調(diào)用,或者自定義一個alertview。
2.跟適配有關(guān)的設(shè)置
iOS8之后,有了UIAlertController這個類,如下
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
很明顯,蘋果強(qiáng)烈建議我們?nèi)绻懿挥肬IAlertView就不要用啦,因為我們有UIAlertController了!
為了兼容iOS7,我們的項目中就統(tǒng)一使用了UIAlertView。問題來了:(項目中的某一)界面中textField處于編輯狀態(tài)(界面上有鍵盤),點(diǎn)擊界面中一個執(zhí)行確定操作的按鈕時,我先將鍵盤收起隨即又執(zhí)行了彈出一個UIAlertView的代碼,點(diǎn)擊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時系統(tǒng)會嘗試恢復(fù)之前的keyboard input??捎幸稽c(diǎn)我不明白的是,我明明執(zhí)行了收起鍵盤的代碼,不知道蘋果是怎么處理的。
有人用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];
}