有時候會有這樣的需求:App 運行完,加載 RootVC ,此時需要做一些操作,比如檢查更新,之類的。此時可能會需要有2個甚至多個彈框依次彈出。
本篇將以系統(tǒng)的 UIAlertController 作為示例,當(dāng)然,如果是自定義的,也要看一下這篇文章,如何來處理多個彈窗。
首先,如果就按照如下的默認(rèn)寫法:
- (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 的子視圖,這么做第二個彈框會蓋在第一個上面。如果你用了毛玻璃背景,效果會更加明顯。肯定不合適了。
所以,正確的解決辦法就是類似加鎖的過程,當(dāng)點擊了第一個彈框的某個按鈕之后,再彈出第二個彈框,以此類瑞。
這里,我想到用信號量去解決,但是信號量會阻塞線程,不可以直接在主線程使用。所以我們需要在子線程控制信號量,在主線程創(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ā)。當(dāng)然也有人故意這么做。對于 “彈框彈出的時間,不要做其他任何事情” 這種需求是很合適的。當(dāng)然我們千萬不能去阻塞主線程!
我們在異步線程等待信號,在主線程發(fā)信號,如此就可以實現(xiàn)兩個線程同步。其實信號量就是一種鎖。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選