【问题标题】:How to move viewcontroller to rootViewController on tabbar item click如何在标签栏项目单击上将 viewcontroller 移动到 rootViewController
【发布时间】:2014-02-12 12:51:25
【问题描述】:

我正在使用 UITabbarControllerUINavigationController 来处理我的应用程序。我已经创建了带有UITabbar 基本应用程序的应用程序,我提供了不同类型的导航控制器来导航视图。如下图所示。

但我有 1 个问题,我描述如下:

假设我有 5 个 ViewController 和 3 个 NavigationViewController 用于所有五个视图控制器,例如 View1 作为rootViewController 和 view2 作为子视图。将 3 视为 RootViewController 用于第二个 NavigationViewController 并将 4 视为第三个 RootViewController 和其他的 subview

当我运行应用程序时 view1 加载为 RootViewController 现在我导航到视图 2,因为它是视图 1 的子视图,当时我的标签栏选择到第一个标签。当我单击第二个标签栏按钮时,它显示我将视图 3 视为RootViewController 用于第二个NavigationController

现在我点击返回到第一个Tabbar 按钮以查看视图 1,但它显示了子视图视图 2。如果我需要移动到 RootViewController,我需要按返回按钮查看我的 RootView

因此,根据我的 sinario,是否可以为该特定标签栏设置 RootView,以便用户可以轻松转到 RootView,而无需查看所有标签栏点击项目的子视图。

请帮帮我。

【问题讨论】:

  • 你的意思是当你点击你的标签栏按钮时,它应该显示你的导航控制器的根视图控制器?
  • 是的@jai,你是对的。

标签: ios uinavigationcontroller uitabbarcontroller uitabbar


【解决方案1】:

创建三个视图控制器(您需要五个),然后将每个视图控制权交给每个单独的导航控制器。然后将三个导航控制器分配给标签栏,如下所示-

RideViewController* rideObj = [[RideViewController alloc]initWithNibName:@"RideViewController" bundle:nil];
RequestARideViewController* requestARideObj = [[RequestARideViewController alloc]initWithNibName:@"RequestARideViewController" bundle:nil];
MyAccountViewController* myAccntObj = [[MyAccountViewController alloc]initWithNibName:@"MyAccountViewController" bundle:nil];


navCtrlObj1 = [[UINavigationController alloc]initWithRootViewController:rideObj];
navCtrlObj2 = [[UINavigationController alloc]initWithRootViewController:requestARideObj];
navCtrlObj3 = [[UINavigationController alloc]initWithRootViewController:myAccntObj]

self._tabBarController = [[UITabBarController alloc]init];
self._tabBarController.delegate=self;
self._tabBarController.viewControllers = [NSArray arrayWithObjects:navCtrlObj1,navCtrlObj2,navCtrlObj3,nil];

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{  
    write your code here to move the ViewController as written below.(as your requirement)


            [navCtrlObj1 popToRootViewControllerAnimated:YES];

            [navCtrlObj2 popToRootViewControllerAnimated:YES];

            [navCtrlObj3 popToRootViewControllerAnimated:YES];



}

【讨论】:

    【解决方案2】:

    使用此代码。当您单击选项卡时,您会看到该选项卡的导航控制器的根视图

     -(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
    {  
    write your code here to move the ViewController as written below.(as your requirement)
    
    [navcontrol1 popToRootViewControllerAnimated:YES];
    [navcontrol2 popToRootViewControllerAnimated:YES];
    [navcontrol3 popToRootViewControllerAnimated:YES];
    
    }
    

    【讨论】:

      【解决方案3】:

      创建三个视图控制器,然后将每个视图控制权交给每个单独的导航控制器。然后将三个导航控制器分配给标签栏。

          self.tabBarController = [[UITabBarController alloc] init];
          [self.tabBarController setDelegate:self];
          self.tabBarController.viewControllers = @[navigationController1, navigationController2,navigationController3];
      
      - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
          {
              write your code here to move the view 
          }
      

      【讨论】:

        猜你喜欢
        • 2016-07-27
        • 1970-01-01
        • 2012-05-07
        • 2022-08-15
        • 1970-01-01
        • 2016-03-30
        • 2018-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多