bug日誌(1):UIAlertView消失之後收合的鍵盤又彈出

來源:互聯網
上載者:User

標籤:

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

 

bug日誌(1):UIAlertView消失之後收合的鍵盤又彈出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.