我有一个从 AppDelegate 创建 tabbarcontroller 的应用程序。我想在导航栏中添加一个按钮,但无法做到。最终我设法掌握了一些工作代码,但我并不真正理解它。
步骤是:
- 确认 AppDelegate 到 UINavigationControllerDelegate
- 设置 rootNavigationController.delegate = self
- 覆盖 navigationController:willShowViewController:animated 和 tabBarController:didSelectViewController
我想我遵循tabBarController: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];
}
}