我的iOS应用有:
TabBarController
NavigationController1
TableView1
ViewController1 (Details View)
NavigationController2
TableView2
ViewController2 (Details View)
的行为:
期望:
执行最后一步时,我想关闭详细信息视图并查看第一个TableView1,当切换回第二个标签时,我希望将其关闭并查看表格视图。
我尝试了dismissViewControllerAnimated
和popToRootViewControllerAnimated
的不同组合,但我似乎无法弄明白。
MainTabBarController.h
@interface MainTabBarController : UITabBarController <UITabBarControllerDelegate>
MainTabBarController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.delegate = self;
}
...
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// NSLog Works fine, and displays information in the output
NSLog (@"%@ %lu", tabBarController.selectedViewController.title, tabBarController.selectedIndex);
// None of the lines below achieve the desired result
[viewController.navigationController popToRootViewControllerAnimated:YES];
[viewController dismissViewControllerAnimated:YES completion:nil];
[tabBarController.navigationController popToRootViewControllerAnimated:YES];
[tabBarController dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:2)
一种选择是使用UITabBarControllerDelegate
。侦听选项卡选择的更改。基于新选项卡,获取选项卡的导航控制器并调用其popToRootViewControllerAnimated:
方法。
根据问题中添加的代码进行更新:
问题在于您如何尝试弹出视图控制器。你想要这个:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
// NSLog Works fine, and displays information in the output
NSLog (@"%@ %lu", tabBarController.selectedViewController.title, tabBarController.selectedIndex);
// If the selected tab's root controller is a navigation controller
// pop to the top view controller in the tab's navigation stack
if ([viewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)viewController;
[nav popToRootViewControllerAnimated:NO];
}
}
答案 1 :(得分:1)
这是一个简单的解决方案。
尝试实现UIViewContorller的以下方法
- (void)viewWillDisappear:(BOOL)animated; // Called when the view is dismissed, covered or otherwise hidden. Default does nothing
- (void)viewDidDisappear:(BOOL)animated; // Called after the view was dismissed, covered or otherwise hidden. Default does nothing
转到detail-1视图控制器并实现方法- (void)viewWillDisappear:(BOOL)animated
。
为该控制器做一个pop。
您应该为detail-2
以下是可以帮助您的代码段。
在 Appdelegate.m
中@interface AppDelegate ()<UITabBarControllerDelegate>
@property(nonatomic, strong) MainTabBarController *rootTabBarController;
@end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.rootTabBarController = [[MainTabBarController alloc]init];
self.rootTabBarController.delegate = self;
self.window.rootViewController = self.rootTabBarController;
[self.window makeKeyAndVisible];
}
TabBarController委托实施
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSUInteger index = [self.rootTabBarController.viewControllers indexOfObject:viewController];
NSLog(@"Index : %lu", (unsigned long)index);
switch (index) {
case 0:
// pop other tab barcontrollers pushed or modal windows
[self.rootTabBarController flushViewControllerStackForIndex:1];
break;
case 1:
[self.rootTabBarController flushViewControllerStackForIndex:0];
break;
default:
break;
}
}
<强> MainTabBarController.m 强>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setViewControllers:@[
[[UINavigationController alloc] initWithRootViewController:[[FirstViewController alloc]init]],
[[UINavigationController alloc] initWithRootViewController:[[SecondViewController alloc]init]]
] animated:YES];
}
-(void)flushViewControllerStackForIndex:(NSUInteger )index {
[[self.viewControllers objectAtIndex:index] popToRootViewControllerAnimated:NO];
}
以下是我跑过的样本的顺序截图。
以下是Sample code.
这应该可以解决你的目的&amp;是正确的方法。
现在您可能需要在flushViewControllerStackForIndex
中微调自己的逻辑,以检查是否只有控制器被推入堆栈或推送和放大器的组合。模态。所以最好尝试在Stack&amp; do-a-dismiss-if-a-modal 或 do-a-pop-if-a-push 。
希望有所帮助。
答案 2 :(得分:0)
您可以直接在导航堆栈上设置当前的视图控制器。您只需在tabbar控制器中切换选项卡时直接设置导航控制器的viewControllers属性。
切换到tab1时设置NavigationController1.viewcontrollers = @ [tableView1]