【问题标题】:Initializing a property of UIViewController from UITabBar从 UITabBar 初始化 UIViewController 的属性
【发布时间】:2014-11-05 15:47:14
【问题描述】:

从 TabBar 中单击时,我需要设置 ViewController 的属性。

我尝试在 didSelectViewController 中执行此操作 - 但在 ViewWillAppear 完成后调用它并且为时已晚

我试过这样做:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

viewController 属于原始tabBarIndex 而不是目标tabBarIndex

我能做什么?

【问题讨论】:

    标签: ios uiviewcontroller uitabbar


    【解决方案1】:

    如果你使用storyboard,你可以设置segue ID然后:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([segue.identifier isEqualToString:YOUR_SEGUE_ID]) {
            YourViewController *controller = segue.destinationViewController;
            controller.someProperty = someProperty;
        }
    }
    

    【讨论】:

      【解决方案2】:

      好吧,在挖掘了代码之后,我找到了一个方法:

      - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
      {
          UINavigationController *nav = (UINavigationController *)viewController;
          if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[DestinationViewController class]])
          {
              DestinationViewController *vc = (DestinationViewController *)[nav.viewControllers objectAtIndex:0];
              vc.foo = 1;
          }
      
          return YES;
      }
      

      【讨论】:

      • 是的,shouldSelect... 是正确的选择。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多