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