2015-09-07 80 views
3

語境:popToRootViewController

我使用TabViewController和NavigationController在同一時間。這兩個選項卡分別是RECENT和​​,它們顯示帖子列表。想象一下,你在RECENT標籤裏面並點擊帖子,然後進入postsShow視圖。所以你在導航堆棧中更深一層。當您轉到​​選項卡並返回到RECENT選項卡時,您仍然可以看到之前單擊的帖子。但我想顯示一個帖子列表,而不是。

我試圖:

我設置PostsShowViewController一個TabBarControllerDelegate當選擇一個選項卡的項目,我想彈出它的根視圖控制器。然後,當用戶回來時,他會看到rootViewController,它是帖子列表而不是PostsShow視圖。

代碼:

viewDidAppear self.tabBarController.delegate = self;

viewDidDisappear self.tabBarController.delegate = nil;

UITabBarControllerDelegate

- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController { 
    [self.navigationController popToRootViewControllerAnimated:NO]; 
    return YES; 
} 

它是如何不起作用:

  1. 轉到最近的標籤
  2. 點擊後進入PostsShow視圖
  3. 轉到熱門標籤
  4. 回去最近標籤(我期待看到帖子列表而不是PostsShow視圖)
  5. 錯誤! EXC_BAD_ACCESS

編輯: 繼答案建議做什麼,我得到一個稍微好一點的行爲,但仍然有錯誤告終。

代碼

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ 
    UINavigationController *navigation = (UINavigationController*) viewController; 
    [navigation popToRootViewControllerAnimated:NO]; 
} 
  1. 去最近的標籤
  2. 點擊後去PostsShowview
  3. 去流行標籤
  4. 回到最近的標籤
  5. 我看到了帖子列表(沒有錯誤!)
  6. 回到流行標籤:ERR_BAD_ACCESS!

編輯: 這是我的故事板 enter image description here

EDIT2:

完整的堆棧跟蹤:

* thread #1: tid = 0x4a37c, 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x10) 
    frame #0: 0x0000000197bb7bd0 libobjc.A.dylib`objc_msgSend + 16 
    frame #1: 0x000000018ab52078 UIKit`-[UITabBarController _tabBarItemClicked:] + 104 
    frame #2: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96 
    frame #3: 0x000000018ab51fb4 UIKit`-[UITabBar _sendAction:withEvent:] + 468 
    frame #4: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96 
    frame #5: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612 
    frame #6: 0x000000018ab51bec UIKit`-[UITabBar(Static) _buttonUp:] + 128 
    frame #7: 0x000000018a9891ec UIKit`-[UIApplication sendAction:to:from:forEvent:] + 96 
    frame #8: 0x000000018a9722c8 UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 612 
    frame #9: 0x000000018a988b88 UIKit`-[UIControl touchesEnded:withEvent:] + 592 
    frame #10: 0x000000018a947da8 UIKit`_UIGestureRecognizerUpdate + 8536 
    frame #11: 0x0000000185e8fff0 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32 
    frame #12: 0x0000000185e8cf7c CoreFoundation`__CFRunLoopDoObservers + 360 
    frame #13: 0x0000000185e8d35c CoreFoundation`__CFRunLoopRun + 836 
    frame #14: 0x0000000185db8f74 CoreFoundation`CFRunLoopRunSpecific + 396 
    frame #15: 0x000000018f8136fc GraphicsServices`GSEventRunModal + 168 
    frame #16: 0x000000018a9bad94 UIKit`UIApplicationMain + 1488 
    * frame #17: 0x0000000100023ff4 toaster-objc`main(argc=1, argv=0x000000016fdeba50) + 124 at main.m:14 
    frame #18: 0x000000019824ea08 libdyld.dylib`start + 4 
+0

你是什麼層次? TabBarViewController - > NavigationController? – Miknash

+0

@NickCatib是的。每個tabItem都連接到一個NavigationController。 –

+0

看到這個鏈接可能會幫助你http://stackoverflow.com/questions/21017985/call-poptorootviewcontroller-from-another-tab –

回答

3

這是我如何在迅速做到了:

func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) { 

    self.tabBarSelectedIndex = tabBarController.selectedIndex 
    var navigation = viewController as! UINavigationController 
    navigation.popToRootViewControllerAnimated(false) 
    // rest of the logic 
} 

在Objective-C相似:我用didSelectViewController方法的UITabBarController

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 
    self.tabBarSelectedIndex = tabBarController.selectedIndex; 
    UINavigationController *navigation = (UINavigationController*) viewController; 
    [navigation popToRootViewControllerAnimated:NO]; 
} 

通知。

您可以檢查它here

+0

感謝您的答案。請看我更新的問題。 –

+0

這可能是因爲你有一個UINavigationController而不是另一個選項卡。你可以只對某個selectedIndex做這個彈出,即if(tabBarController.selectedIndex == 0)轉換爲導航和彈出。 – Miknash

+0

另外,你可以啓用異常斷點來確定崩潰的位置 – Miknash

0

試試這個:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{ 

if ([viewController isKindOfClass:[UINavigationController class]]) 
{ 
    UINavigationController *navController = (UINavigationController *)viewController; 

    [navController popToRootViewControllerAnimated:NO]; 

} 
} 
+0

感謝您的回答。請看我更新的問題。 –

+0

可能是你沒有在熱門標籤上的導航控制器。如果條件如我在我的回答 如果([viewController isKindOfClass:[UINavigationController class]]) – coder1010

+0

我提到(請參閱更新的問題),我試圖添加條件:( –

0
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController 
{ 
    if ([viewController isKindOfClass:[UINavigationController class]]) { 
     [(UINavigationController *)viewController popToRootViewControllerAnimated:YES]; 
    } 
} 
相關問題