ObjC 如何在选择 TabBarItem 时弹出回 RootViewController
ObjC how to pop back to RootViewController when TabBarItem is selected
我得到了以下流程
在主页选项卡 Select 晚上叫 [self.parentViewController.tabBarController setSelectedIndex:2];
去 Class 选项卡
在 Class 选项卡 select 任何 Class
进入下一个VC
Select又是回家又是傍晚
停留在之前VC没有回到RootView
应该在这里
从 Home Tab
我执行 [self.parentViewController.tabBarController setSelectedIndex:2];
到 Class Tab
。然后从嵌入在 NavigationController
中的 Class 选项卡,然后我将使用 seague
转到下一个 VC 及以后。
但是当我再次 select Home Tab
时,我希望 Class Tab
回到 RootViewController
我试过以下方法,但没有用。每次下一个 VC
消失时,它都会一直弹出到 RootViewController
。
-(void) viewWillDisappear:(BOOL)animated {
[self.navigationController popViewControllerAnimated:YES];
[super viewWillDisappear:animated];
}
我有以下代码 MyTabBarController,它是一位友善的堆栈溢出大师给我的,但不确定每次新的 TabBarController
是 select编辑。请帮忙。
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"didSelectViewController... ");
//==== Tried this but not triggering =====
//[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
//if ([viewController isKindOfClass:[UINavigationController class]]) {
//[self.navigationController popViewControllerAnimated:YES];
//}
//==========================================
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
if ([viewController isKindOfClass:[UINavigationController class]]) {
// we're expecting a nav controller so cast it to a nav here
UINavigationController *navController = (UINavigationController *)viewController;
// now grab the first view controller from that nav controller
UIViewController *firstViewControllerInNav = navController.viewControllers.firstObject;
// check to make sure it's what we're expecting (ParentViewController)
if ([firstViewControllerInNav isKindOfClass:[ParentViewController class]]) {
// cast it to our parent view controller class
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
ParentViewController *viewControllerToCallMethodOnAfterSelection = (ParentViewController *)firstViewControllerInNav;
[viewControllerToCallMethodOnAfterSelection doStuffWhenTabBarControllerSelects];
}else{
//=== The following code will make viewWillAppear load on each tab bar item
//=== Without it, tapping on new tab bar item will not load viewWillAppear
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
}
添加了以下代码,它会触发但不会将 selectedIndex = 2 带回 Root
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
//Check if current index is Class tab and new index is Home
if (tabBarController.selectedIndex == 2 && shouldSelectIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
//[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
//[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex];
}
return YES;
}
已添加故事板
添加文件结构
如果我理解正确,那么如果当前选项卡是 Class
并且选择了 Home
选项卡,您想返回到根视图。也许这会比使用 didSelectViewController
:
更好
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
//Check if current index is Class tab and new index is Home
if (tabBarController.selectedIndex == 2 && shouldSelectIndex == 0) {
//Pop to root code with `[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]` (Class tab's navigation controller)
}
return YES;
}
使用下面的swift代码清除导航栏控制器数组
guard let child = self.childNavController else {return}
child.viewControllers = []
您可以在 objective C 中使用相同的选项卡栏选择方法
在您的 tabbarController 中创建一个方法,并在您希望 class 选项卡访问其根视图控制器时调用此方法
-(void)popToClassRootViewController{
// Considering the fact that class view contorller will always be on 3 no and will be of UINavigationController
UINavigationController *classNavController = (UINavigationController *)[self.viewControllers objectAtIndex:2];
// You can get the navigation controller reference by any way you prefer
[classNavController popToRootViewControllerAnimated:false];
}
在你的情况下,我认为你想要在单击其他选项卡时重置视图控制器,这样你就可以使用 tabbar 委托方法来检查是否单击了其他选项卡栏项目并调用该方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
if (index != 2) {
//Note: Call this method according to your need in this case it will be called whenever user will select tab other then Class
[self popToClassRootViewController];
}
return true;
}
我得到了以下流程
在主页选项卡 Select 晚上叫 [self.parentViewController.tabBarController setSelectedIndex:2];
去 Class 选项卡
在 Class 选项卡 select 任何 Class
进入下一个VC
Select又是回家又是傍晚
停留在之前VC没有回到RootView
应该在这里
从 Home Tab
我执行 [self.parentViewController.tabBarController setSelectedIndex:2];
到 Class Tab
。然后从嵌入在 NavigationController
中的 Class 选项卡,然后我将使用 seague
转到下一个 VC 及以后。
但是当我再次 select Home Tab
时,我希望 Class Tab
回到 RootViewController
我试过以下方法,但没有用。每次下一个 VC
消失时,它都会一直弹出到 RootViewController
。
-(void) viewWillDisappear:(BOOL)animated {
[self.navigationController popViewControllerAnimated:YES];
[super viewWillDisappear:animated];
}
我有以下代码 MyTabBarController,它是一位友善的堆栈溢出大师给我的,但不确定每次新的 TabBarController
是 select编辑。请帮忙。
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(@"didSelectViewController... ");
//==== Tried this but not triggering =====
//[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
//if ([viewController isKindOfClass:[UINavigationController class]]) {
//[self.navigationController popViewControllerAnimated:YES];
//}
//==========================================
NSLog(@"controller class: %@", NSStringFromClass([viewController class]));
NSLog(@"controller title: %@", viewController.title);
if ([viewController isKindOfClass:[UINavigationController class]]) {
// we're expecting a nav controller so cast it to a nav here
UINavigationController *navController = (UINavigationController *)viewController;
// now grab the first view controller from that nav controller
UIViewController *firstViewControllerInNav = navController.viewControllers.firstObject;
// check to make sure it's what we're expecting (ParentViewController)
if ([firstViewControllerInNav isKindOfClass:[ParentViewController class]]) {
// cast it to our parent view controller class
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
ParentViewController *viewControllerToCallMethodOnAfterSelection = (ParentViewController *)firstViewControllerInNav;
[viewControllerToCallMethodOnAfterSelection doStuffWhenTabBarControllerSelects];
}else{
//=== The following code will make viewWillAppear load on each tab bar item
//=== Without it, tapping on new tab bar item will not load viewWillAppear
[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
}
}
}
添加了以下代码,它会触发但不会将 selectedIndex = 2 带回 Root
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
//Check if current index is Class tab and new index is Home
if (tabBarController.selectedIndex == 2 && shouldSelectIndex == 0) {
[self.navigationController popViewControllerAnimated:YES];
//[(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
//[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex];
}
return YES;
}
已添加故事板
添加文件结构
如果我理解正确,那么如果当前选项卡是 Class
并且选择了 Home
选项卡,您想返回到根视图。也许这会比使用 didSelectViewController
:
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger shouldSelectIndex = [tabBarController.viewControllers indexOfObject:viewController];
//Check if current index is Class tab and new index is Home
if (tabBarController.selectedIndex == 2 && shouldSelectIndex == 0) {
//Pop to root code with `[tabBarController.viewControllers objectAtIndex:tabBarController.selectedIndex]` (Class tab's navigation controller)
}
return YES;
}
使用下面的swift代码清除导航栏控制器数组
guard let child = self.childNavController else {return}
child.viewControllers = []
您可以在 objective C 中使用相同的选项卡栏选择方法
在您的 tabbarController 中创建一个方法,并在您希望 class 选项卡访问其根视图控制器时调用此方法
-(void)popToClassRootViewController{
// Considering the fact that class view contorller will always be on 3 no and will be of UINavigationController
UINavigationController *classNavController = (UINavigationController *)[self.viewControllers objectAtIndex:2];
// You can get the navigation controller reference by any way you prefer
[classNavController popToRootViewControllerAnimated:false];
}
在你的情况下,我认为你想要在单击其他选项卡时重置视图控制器,这样你就可以使用 tabbar 委托方法来检查是否单击了其他选项卡栏项目并调用该方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSUInteger index = [tabBarController.viewControllers indexOfObject:viewController];
if (index != 2) {
//Note: Call this method according to your need in this case it will be called whenever user will select tab other then Class
[self popToClassRootViewController];
}
return true;
}