我使用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;
}
它是如何不起作用:
- 轉到最近的標籤
- 點擊後進入PostsShow視圖
- 轉到熱門標籤
- 回去最近標籤(我期待看到帖子列表而不是PostsShow視圖)
- 錯誤!
EXC_BAD_ACCESS
編輯: 繼答案建議做什麼,我得到一個稍微好一點的行爲,但仍然有錯誤告終。
代碼
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
UINavigationController *navigation = (UINavigationController*) viewController;
[navigation popToRootViewControllerAnimated:NO];
}
- 去最近的標籤
- 點擊後去PostsShowview
- 去流行標籤
- 回到最近的標籤
- 我看到了帖子列表(沒有錯誤!)
- 回到流行標籤:ERR_BAD_ACCESS!
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
你是什麼層次? TabBarViewController - > NavigationController? – Miknash
@NickCatib是的。每個tabItem都連接到一個NavigationController。 –
看到這個鏈接可能會幫助你http://stackoverflow.com/questions/21017985/call-poptorootviewcontroller-from-another-tab –