国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS實現多個彈框按順序依次彈出效果

2019-10-21 18:40:09
字體:
來源:轉載
供稿:網友

有時候會有這樣的需求:App 運行完,加載 RootVC ,此時需要做一些操作,比如檢查更新,之類的。此時可能會需要有2個甚至多個彈框依次彈出。

本篇將以系統的 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>

所以說,第二個彈框應該是看不到的。

另一種情況,如果是自定義的 Alert ,你把它 add 為 window 的子視圖,這么做第二個彈框會蓋在第一個上面。如果你用了毛玻璃背景,效果會更加明顯。肯定不合適了。

所以,正確的解決辦法就是類似加鎖的過程,當點擊了第一個彈框的某個按鈕之后,再彈出第二個彈框,以此類瑞。

這里,我想到用信號量去解決,但是信號量會阻塞線程,不可以直接在主線程使用。所以我們需要在子線程控制信號量,在主線程創建和顯示 Alert,直接上代碼。

- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //創建一個隊列,串行并行都可以,主要為了操作信號量 dispatch_queue_t queue = dispatch_queue_create("com.se7en.alert", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{  //創建一個初始為0的信號量  dispatch_semaphore_t sema = dispatch_semaphore_create(0);  //第一個彈框,UI的創建和顯示,要在主線程  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上的按鈕,我們發送一次信號。    dispatch_semaphore_signal(sema);   }]];   [self presentViewController:alert animated:YES completion:nil];  });  //等待信號觸發,注意,這里是在我們創建的隊列中等待  dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);  //上面的等待到信號觸發之后,再創建第二個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];  });  //同理,創建第三個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];  }); });}

如此一來,就實現了我們的需求。

需要注意的是,這里為什么不用全局并發隊列,主要是考慮到信號量會阻塞線程,優先級特別高,如果此時隊列中還有任務,那么就會等待信號觸發。當然也有人故意這么做。對于 “彈框彈出的時間,不要做其他任何事情” 這種需求是很合適的。當然我們千萬不能去阻塞主線程!

我們在異步線程等待信號,在主線程發信號,如此就可以實現兩個線程同步。其實信號量就是一種鎖。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 延寿县| 平安县| 宁南县| 安泽县| 长汀县| 大石桥市| 清流县| 彩票| 新干县| 读书| 蒲江县| 民丰县| 乌苏市| 昌平区| 怀柔区| 惠水县| 循化| 中超| 景德镇市| 怀仁县| 云梦县| 沙雅县| 英超| 尼玛县| 阳曲县| 东山县| 晋城| 博白县| 苍山县| 江永县| 韶关市| 茶陵县| 辽阳县| 循化| 顺义区| 浦北县| 长子县| 柘荣县| 普安县| 磴口县| 库尔勒市|