iOS7 連續pop導致崩潰的問題解決

現在很多APP最低還是需要支持iOS7系統,而iOS7卻有很多的BUG,其中NavigtaionController的Push和Pop 與iOS8及以后系統,有著不一樣的處理。
今天在FirstViewController中使用了一個Block執行了[weakSelf.navigationController popToRootViewControllerAnimated:YES];,用來處理,當返回的值為空時就pop到上一個頁面。

scanBarCodeVC.complete = ^(NSString *quthCodeStr) {
                
                if (0 >= [quthCodeStr length]) {
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                    });
                    
                    return;
                }
                
                // TODO Others
            };

在SecondViewController中獲取到quthCodeStr的值后,調用complete這個block

if (nil != self.complete) {
                self.complete(metadataObj.stringValue);
                self.complete = nil;
                
                __weak typeof(self) weakSelf = self;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                
            } else {
                return;
            }

此時獲取到了quthCodeStr回調完complete這個block后,就pop到上一個頁面。** 此時在iOS7 的系統下就會崩潰** 會出現提示 Finishing up a navigation transition in an unexpected state. Navigation Bar 分析這個原因應該為

前一個pop還沒有結束時,進行了另一個pop操作,而在iOS7系統中對此沒有做優化,不能正常處理,所以崩潰了。


解決方法:

scanBarCodeVC.complete = ^(NSString *quthCodeStr) {
                
                if (0 < [quthCodeStr length]) {
                    // Must use this metod for compareing the ios7
                    NSMutableArray *vcs = [[NSMutableArray alloc] initWithArray:weakSelf.navigationController.viewControllers];
                    for (UIViewController *vc in vcs) {
                        if ([vc isKindOfClass:[weakSelf class]]) {
                            [vcs removeObject:vc];
                            
                            break;
                        }
                    }
                    
                    weakSelf.navigationController.viewControllers = vcs;
                }
                
                // TODO Others
            };

采用當pop的時候,先將當前的viewcontroller從navigationController中viewControllers數組里刪除,然后在SecondViewController里跳轉的時候,就直接回調到了FirstViewController的上一個VC中。

// END

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容