iOS實現(xiàn)多個彈框按順序依次彈出效果
有時候會有這樣的需求:App 運行完,加載 RootVC ,此時需要做一些操作,比如檢查更新,之類的。此時可能會需要有2個甚至多個彈框依次彈出。
本篇將以系統(tǒng)的 UIAlertController 作為示例,當然,如果是自定義的,也要看一下這篇文章,如何來處理多個彈窗。
首先,如果就按照如下的默認寫法:
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"彈框1" message:@"第一個彈框" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert animated:YES completion:nil]; UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"彈框2" message:@"第二個彈框" preferredStyle:UIAlertControllerStyleAlert]; [alert2 addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [self presentViewController:alert2 animated:YES completion:nil]; }
會有什么問題呢?注意控制臺,肯定會輸出
Warning: Attempt to present <UIAlertController: 0x7ff4c3078c00> on <SCTestViewController: 0x7ff4c2718c20> which is already presenting <UIAlertController: 0x7ff4c283ae00>
所以說,第二個彈框應(yīng)該是看不到的。
另一種情況,如果是自定義的 Alert ,你把它 add 為 window 的子視圖,這么做第二個彈框會蓋在第一個上面。如果你用了毛玻璃背景,效果會更加明顯。肯定不合適了。
所以,正確的解決辦法就是類似加鎖的過程,當點擊了第一個彈框的某個按鈕之后,再彈出第二個彈框,以此類瑞。
這里,我想到用信號量去解決,但是信號量會阻塞線程,不可以直接在主線程使用。所以我們需要在子線程控制信號量,在主線程創(chuàng)建和顯示 Alert,直接上代碼。
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //創(chuàng)建一個隊列,串行并行都可以,主要為了操作信號量 dispatch_queue_t queue = dispatch_queue_create("com.se7en.alert", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ //創(chuàng)建一個初始為0的信號量 dispatch_semaphore_t sema = dispatch_semaphore_create(0); //第一個彈框,UI的創(chuàng)建和顯示,要在主線程 dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"彈框1" message:@"第一個彈框" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { //點擊Alert上的按鈕,我們發(fā)送一次信號。 dispatch_semaphore_signal(sema); }]]; [self presentViewController:alert animated:YES completion:nil]; }); //等待信號觸發(fā),注意,這里是在我們創(chuàng)建的隊列中等待 dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); //上面的等待到信號觸發(fā)之后,再創(chuàng)建第二個Alert dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"彈框2" message:@"第二個彈框" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { dispatch_semaphore_signal(sema); }]]; [self presentViewController:alert animated:YES completion:nil]; }); //同理,創(chuàng)建第三個Alert dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_main_queue(), ^{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"彈框3" message:@"第三個彈框" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { dispatch_semaphore_signal(sema); }]]; [self presentViewController:alert animated:YES completion:nil]; }); }); }
如此一來,就實現(xiàn)了我們的需求。
需要注意的是,這里為什么不用全局并發(fā)隊列,主要是考慮到信號量會阻塞線程,優(yōu)先級特別高,如果此時隊列中還有任務(wù),那么就會等待信號觸發(fā)。當然也有人故意這么做。對于 “彈框彈出的時間,不要做其他任何事情” 這種需求是很合適的。當然我們千萬不能去阻塞主線程!
我們在異步線程等待信號,在主線程發(fā)信號,如此就可以實現(xiàn)兩個線程同步。其實信號量就是一種鎖。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在uiview 的tableView中點擊cell進入跳轉(zhuǎn)到另一個界面的實現(xiàn)方法
這篇文章主要介紹了在uiview 的tableView中點擊cell進入跳轉(zhuǎn)到另一個界面的實現(xiàn)方法,首先重寫uiviewcontrol方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-10-10iOS實現(xiàn)自動循環(huán)播放的banner實例詳解
輪播視圖通常也叫Banner,90%以上App都會用到的一個控件,網(wǎng)上有很多開源代碼,下面這篇文章主要給大家介紹了關(guān)于利用iOS如何實現(xiàn)自動循環(huán)播放的banner的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下。2017-12-12iOS開發(fā)中使用UIDynamic來捕捉動畫組件的重力行為
這篇文章主要介紹了iOS開發(fā)中使用UIDynamic來捕捉動畫組件的重力行為的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用
這篇文章主要介紹了iOS App設(shè)計模式開發(fā)中對interpreter解釋器模式的運用,示例為傳統(tǒng)的Objective-C寫成,需要的朋友可以參考下2016-04-04