Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么选项卡栏需要对navigationController:willShowViewController:animated进行覆盖_Ios_Uinavigationcontroller_Uitabbarcontroller - Fatal编程技术网

Ios 为什么选项卡栏需要对navigationController:willShowViewController:animated进行覆盖

Ios 为什么选项卡栏需要对navigationController:willShowViewController:animated进行覆盖,ios,uinavigationcontroller,uitabbarcontroller,Ios,Uinavigationcontroller,Uitabbarcontroller,我有一个从AppDelegate创建tabbarcontroller的应用程序。我想在导航栏上添加一个按钮,但无法。最终我设法掌握了一些工作代码,但我并不真正理解它 这些步骤是: 确认向UINavigationControllerDelegate发送的AppDelegate 设置rootNavigationController.delegate=self 覆盖navigationController:willShowViewController:animated和tabBarController

我有一个从AppDelegate创建tabbarcontroller的应用程序。我想在导航栏上添加一个按钮,但无法。最终我设法掌握了一些工作代码,但我并不真正理解它

这些步骤是:

  • 确认向UINavigationControllerDelegate发送的AppDelegate
  • 设置rootNavigationController.delegate=self
  • 覆盖navigationController:willShowViewController:animated和tabBarController:didSelectViewController
  • 我想我遵循了选项卡controller:didSelectViewController代码,但对navigationController:willShowViewController:animated所发生的事情迷失了方向

    - (void) tabBarController: (UITabBarController*) tabBarController didSelectViewController: (UIViewController*) viewController
    {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        {
         self.tabBarController.navigationItem.title = viewController.navigationItem.title;
         self.tabBarController.navigationItem.rightBarButtonItems = viewController.navigationItem.rightBarButtonItems;
         self.tabBarController.navigationItem.leftBarButtonItems = viewController.navigationItem.leftBarButtonItems;
         }
      }
    
    
    
    - (void) navigationController: (UINavigationController*) navigationController
       willShowViewController: (UIViewController*) viewController
                     animated: (BOOL) animated
        {
          if (viewController == tabBarController)
          {
            UIViewController* tabViewController = tabBarController.selectedViewController;
        SEL willShowSel = @selector(navigationController:willShowViewController:animated:);
    
          if ([tabViewController respondsToSelector: willShowSel])
          {
            UIViewController<UINavigationControllerDelegate>* vc =
                (UIViewController<UINavigationControllerDelegate>*) tabViewController;
            [vc navigationController: navigationController willShowViewController: vc animated: animated];
          }
      }
    
    -(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController
    {
    如果(用户界面习惯用法()==UIUserInterfaceIdiomPhone)
    {
    self.tabBarController.navigationItem.title=viewController.navigationItem.title;
    self.tabBarController.navigationItem.RightBarButtonims=viewController.navigationItem.RightBarButtonims;
    self.tabBarController.navigationItem.LeftBarButtonims=viewController.navigationItem.LeftBarButtonims;
    }
    }
    -(void)导航控制器:(UINavigationController*)导航控制器
    willShowViewController:(UIViewController*)viewController
    动画:(BOOL)动画
    {
    if(viewController==tabBarController)
    {
    UIViewController*tabViewController=tabBarController.selectedViewController;
    SEL willShowSel=@选择器(导航控制器:WILLSHOWVIEW控制器:动画:);
    if([tabViewController响应选择器:willShowSel])
    {
    UIViewController*vc=
    (UIViewController*)选项卡ViewController;
    [vc navigationController:navigationController将显示视图控制器:vc动画:动画];
    }
    }
    
    此代码可能处理在
    UINavigationController
    内使用
    uitabarcontroller
    时出现的问题。
    uitabarcontroller
    文档说明它需要是根视图控制器(即不在
    UINavigationController
    内)以其他方式使用它可能会导致问题

    代码似乎在做的是捕获通常传递给
    viewController
    的事件,检查它是否是
    uitabarcontroller
    ,如果是,则检查
    uitabarcontroller
    中的可见视图是否响应此方法,如果是,则传递方法(选择器)请注意这一点


    如果可能的话,我建议将
    uitabarcontroller
    UINavigationController
    中取出。这可能需要一些工作,但会使您的代码兼容。(并且不再需要导航控制器:willShowViewController:动画:

    谢谢你的建议。我本以为这是一个相当标准的设计-选项卡栏是顶层,导航栏带有一个按钮。我认为这比这个简单:目的似乎是将按钮添加到TBC的导航栏,但TBC没有导航栏;只有子控制器可以。