一句話筆記,某段時間內(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/