一句話筆記(24)(連續(xù) Push)

一句話筆記,某段時間內(nèi)遇到或看到的某個可記錄的點。 2017-05-09

  • 1、如何確定是 Tab 切換的頁面 還是 Pop 回來的頁面
  • 2、解決在某些情況下出現(xiàn)連續(xù) Push 的情況的 BUG

一、如何確定是 Tab 切換的頁面 還是 Pop 回來的頁面

  • 思路一: 從 UITabBarControllerDelegate 處判斷
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UINavigationController *)viewController {
    if ([viewController.topViewController isKindOfClass:TestViewController.class]) {
        TestViewController *testVC = (TestViewController *)viewController.topViewController;
        testVC.isTabBarSwitch = YES;
    }
    return YES;
}
  • 思路二: 從 重寫 UINavigationController Pop 判斷
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    self.isPop = YES;
    return [super popViewControllerAnimated:animated];
}

然后想了下,還是選擇了思路二,畢竟這樣假如后期其他 ViewController 也有用到其的時候,方便擴展。

二、解決在某些情況下出現(xiàn)連續(xù) Push 的情況的 BUG

有時候,我們在網(wǎng)絡條件比較差的情況下,容易出現(xiàn)那種連續(xù) Push 的情況,讓人很煩。

  • 解決方案一:在其代理中通過一個 臨時變量 判斷
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    if (self.isSwitching) {
        return;
    }
    self.isSwitching = YES;
    [super pushViewController:viewController animated:animated];
}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    self.isSwitching = YES;
//}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.isSwitching = NO;
}

注意下面那個方法是代理呈現(xiàn)出來的。

  • 解決方案二: 通過判斷當前 ViewController 來解決
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判斷上一個控制器和現(xiàn)在的控制器是不是同一個
    if ([self.topViewController isMemberOfClass:[viewController class]]) {
        return;
    }
    [super pushViewController:viewController animated:animated];
}

通常情況下,解決 網(wǎng)絡比較差的情況下出現(xiàn)的連續(xù) Push, 個人認為采取方案二解決。

PS: 防止觸摸事件觸發(fā)新方法的一個系統(tǒng)方法。


//開始禁止交互  
- (void)beginIgnoringInteractionEvents; 
//結(jié)束禁止交互  
- (void)endIgnoringInteractionEvents;  
//是否禁止了交互  
- (BOOL)isIgnoringInteractionEvents;    

所以此處在某些 Target-Action 處也可以直接,防止在這個過程中的第二次交互:

- (IBAction)testAction:(id)sender {
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    // Do Some
    FirstViewController * firstVC = [[FirstViewController alloc] init];
    [self.navigationController pushViewController:firstVC animated:YES];
    
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
}

但是實際上這樣是不能很好的解決的 連續(xù)Push 的過程,它只是保證在每一次 Action 的時候外部禁止交互, 而網(wǎng)絡環(huán)境比較弱的時候,這個過程都還沒開始,卻被執(zhí)行了多遍 Action, 從而其實上并沒有解決這個問題。所以,此處有待商榷。

所以,目前個人暫時比較認同方案二的, 簡單直接。

  • 當然如果項目中出現(xiàn)一個類真需連續(xù)調(diào)用的情況,那就另當別論。別說,還真有這種情況。
  • 而采用方案一也有一個問題,當其是 RootViewController 的時候, 那個 方法會走兩次,所以也無效。
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

于是最終,我是為了解決 網(wǎng)絡條件差的情況下,偶爾出現(xiàn)的連續(xù) Push ,所以我采用兩者合一。

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 對于同一個 ViewController 防止連續(xù) Push
    if ([self.topViewController isMemberOfClass:[viewController class]]) {
        if (self.isSwitching) {
            return;
        }
        self.isSwitching = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    self.isSwitching = NO;
}

備注參考:
http://blog.lessfun.com/blog/2015/09/09/uiviewcontroller-push-pop-trap/

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評論 25 708
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,842評論 4 61
  • JSON 簡介: json 是JavaScript Object Notation的縮寫,它是一種數(shù)據(jù)交換格式。 ...
    1693c7e88afe閱讀 181評論 0 0
  • 愛蜜部落簡筆畫線條魔法之《戴花的少女 》
    香香靜夢雨緣閱讀 221評論 0 1
  • 我想念 聒噪的蟬鳴 急驟的暴雨 氤氳的暑氣 溫涼的晚風 我憶起 池塘的鴨子 它飛向哪里 又落于何處 我念及 曾經(jīng)的...
    Gooody閱讀 126評論 0 0

友情鏈接更多精彩內(nèi)容