【问题标题】:iphone app - uitabbarcontroller to always select first view in a uinavigationcontroller?iphone app - uitabbarcontroller 总是在 uinavigationcontroller 中选择第一个视图?
【发布时间】:2012-06-04 21:39:53
【问题描述】:

我正在构建一个 iphone 应用程序。

我有一个标签栏控制器,它有 2 个标签项。每个 tabitem 链接到不同的导航控制器。每个导航控制器链接到表视图控制器的层次结构。 当用户单击选项卡 1,然后单击表中的项目,然后单击选项卡 2,然后单击选项卡 1,应用程序会显示他在单击选项卡 2 之前正在查看的表。

我如何让应用程序在他每次单击选项卡 1 时显示选项卡 1 的第一个表格,而不是显示他在离开 tab1 之前查看的最新表格?

我更喜欢编程解决方案,而不是使用 xcode 故事板。但如果不存在,那么情节提要解决方案也可以。

【问题讨论】:

    标签: iphone uinavigationcontroller tabbarcontroller


    【解决方案1】:

    当 TabBarController 更改正在显示的选项卡时,在 NavigationController 上调用 popToRootViewControllerAnimated:

    【讨论】:

      【解决方案2】:

      试试这个基本示例,从头开始为每个 UItabBarItem 创建一个 UItabBar 和 UInavigationController:

      在您的头文件 (appdelegate.h) 中,添加此委托:

      @interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
      

      在名为“didFinishLaunchingWithOptions”的函数中,添加这部分代码:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      
      self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      
      UINavigationController *navController=[[UINavigationController alloc] init];
      m_ViewController1 = [[ViewController1 alloc] initWithNibName:@"ViewController1" bundle:nil]; 
      [navController pushViewController:m_ViewController1 animated:NO];
      
      UINavigationController *navController2=[[UINavigationController alloc] init];
      m_ViewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil]; 
      [navController pushViewController:m_ViewController2 animated:NO];
      
      UITabBarController *mtabBarController = [[UITabBarController alloc] init];
      mtabBarController.view.frame = CGRectMake(0, 0, 320, 460);
      
      // Set each tab to show an appropriate view controller
      [mtabBarController setViewControllers:  [NSArray arrayWithObjects:navController1,navController1,navController2, nil]];
      
      self.window.rootViewController = mtabBarController;    
      mtabBarController.delegate = self;
      [self.window makeKeyAndVisible];
      
      return YES;
      }
      

      那么在这个函数中,别忘了添加popToRootViewControllerAnimated函数:

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

      【讨论】:

      • 函数- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController出现在哪个文件中?
      【解决方案3】:

      在我的 appdelegate.h 文件中,我更改了行

      @interface wscAppDelegate : UIResponder <UIApplicationDelegate>
      

      @interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
      

      然后在 viewDidLoad 函数的 CustomTabBarController 中,我添加了以下几行:

      wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
      self.delegate = appDelegate;
      

      然后在 appdelegate.m 文件中,我添加了这个函数

      - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
      {
          for(int c=0; c<[tabBarController.viewControllers count]; c++)
          {
              UINavigationController * navcontroller = [tabBarController.viewControllers objectAtIndex:c];
      
              [navcontroller popToRootViewControllerAnimated:YES];
          }    
      
          return YES;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        • 2015-06-29
        • 2010-11-03
        • 2012-01-10
        • 2020-05-22
        相关资源
        最近更新 更多