iOS8之后,有了UIAlertController這個類,如下
NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
很明顯,蘋果強烈建議廣大碼農們如果能不用UIAlertView就不要用啦,因為我們有UIAlertController了!
進入正題......
為了兼容iOS7,我們的項目中就統一使用了UIAlertView。問題來了:(項目中的某一)界面中textField處於編輯狀態(界面上有鍵盤),點擊界面中一個執行確定操作的按鈕時,我先將鍵盤收起,鍵盤收起的執行代碼是這樣的
[self.view endEditing:YES];
隨即又執行了彈出一個UIAlertView的代碼,點擊UIAlertView上的確定按鈕之后其被dismiss掉了。這時,鍵盤又神奇般的彈了出來!!!
看了一下真機系統版本號:9.2.1。隨后又用另外一個真機(系統版本8.1.2)進行測試時,這個問題卻沒有出現。
於是百度了一下,經驗證,以下代碼可以解決我遇到的這個問題
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。可有一點我不明白的是,我明明執行了收起鍵盤的代碼啊!!! 蘋果,你告訴我,這算不算是一個系統級的bug......
接昨天的寫。在百度的時候,還有一位朋友問了這么一個問題:A界面有個textview,在編輯狀態(有鍵盤)的時候我點擊按鈕,按鈕的處理有結束所有控件編輯(關閉鍵盤)的操作,然后再彈出alertview,點擊alerview對話框后pop到B界面,現在發現pop到B界面后界面會出現彈出鍵盤再消失的情況,如果我去掉彈出alertview這個操作,點擊按鈕后直接pop則不會出現這個問題.
這個,本人親自試驗了一下,發現這個現象也是高版本系統在處理UIAlertView時所表現出來的現象。
有大俠說用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]; }
好了,就寫到這里吧!
從這一篇開始,我將整個iOS開發bug報告系列,歡迎大家關注!
本文參考:http://www.cnblogs.com/android-wuwei/p/4685960.html
http://blog.csdn.net/ul123dr/article/details/50385929
http://www.cocoachina.com/bbs/read.php?tid=307336&page=e&#a
