ios - 如何将rightBarButtonItem传递给TabBarController中的所有ViewController?

标签 ios uinavigationcontroller uitabbarcontroller appdelegate rightbarbuttonitem

我已经构建了一个具有大约 12 个 View 的应用程序。在导航栏的右上角应该有 UIBarButton,它对整个应用程序具有完全相同的功能。它显示了一个带有角标(Badge)的自定义按钮。

我通过 TableViewControllers 向下传递 rightBarButtonItem,它运行良好。但当我点击 TabBar 时,自定义按钮有时会消失。可能我的解决方案很糟糕。我尝试从 AppDelegate 设置自定义按钮,但我不知道如何在与 TabBar 相关的每个 ViewController 上访问 rightBarButtonItem。

我尝试过类似的方法,但它总是在日志中显示 nil。

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{

    NSLog(@"item %@", self.tabController.selectedViewController.navigationItem.rightBarButtonItem);
    NSLog(@"item %@", self.tabController.selectedViewController.navigationItem.rightBarButtonItems);
    NSLog(@"item %@", self.tabController.selectedViewController.navigationController.navigationItem.rightBarButtonItems);
    //self.tabController.selectedViewController.navigationItem.rightBarButtonItem =

    [self switchTabBarImage:self.tabController.selectedIndex];
}

实现“全局”rightBarButtonItem 之类的最佳方法是什么? AppDelegate 是合适的地方吗?

最佳答案

请注意,我的原始答案如下,这需要让选项卡栏 Controller 创建一个选项卡栏按钮,该按钮由选项卡栏 Controller 中包含的所有 Controller 的所有导航栏使用,仅在您创建时才有效一个简单的UIBarButtonItem。更好(如果您使用带有图像等的自定义 UIBarButtonItem 也是至关重要的),是创建一个具有您想要的行为的新的栏按钮项子类,然后将其添加到您的所有项目中。 Controller 的导航栏。我对您后续问题的回答:Custom rightBarButtonItem disappearing示例实现。

为了历史目的,我将在下面保留我的原始答案。


就我个人而言,我认为应用程序委托(delegate)不是正确的地方(尽管在实践中,这可能并不重要)。我个人会继承 UITabBarController 并将其放在那里,然后让 subview 从那里抓取它。例如,子类 UITabBarController 的接口(interface)可能如下所示:

//  MyTabBarController.h

@interface MyTabBarController : UITabBarController

@property (nonatomic, strong) UIBarButtonItem *sharedRightButton;

@end

使用创建按钮的实现(并具有按下该按钮时调用的方法):

// MyTabBarController.m

@implementation MyTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // you'll do your own, fancy button instead of this simple bordered button

    self.sharedRightButton = [[UIBarButtonItem alloc] initWithTitle:@"Test"
                                                              style:UIBarButtonItemStyleBordered 
                                                             target:self
                                                             action:@selector(clickedTest:)];
}

// you'll have your own action method here

- (void)clickedTest:(id)sender
{
    NSLog(@"%s", __FUNCTION__);
}

@end

最后,标签栏 Controller 呈现的每个 View Controller 都可以执行以下操作:

@implementation FirstTabViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MyTabBarController *tabBarController = (MyTabBarController *)self.tabBarController;

    self.navigationItem.rightBarButtonItem = tabBarController.sharedRightButton;
}

@end

关于ios - 如何将rightBarButtonItem传递给TabBarController中的所有ViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13427040/

相关文章:

ios - 在 Swift 中使用 NSNotificationCenter 在选项卡栏 Controller 之间传递数据

iphone - 在项目中实现 UItabbarController + UInavigationBar

iphone - UIViewController 的 navigationController 为 nil

ios - 当我按下后退按钮时出现 Uinavigation 错误

iOS7 UITableViewCell 组

ios - 从代码中呈现 tabBarController 会删除导航栏按钮和标题

ios - 在 StoryBoard 的 TabController 内部使用 Navigation。

ios - 解析不保存字段

ios - 模态 UINavigationController - 我无法停止旋转

ios - 如何在 UITextView 中显示可点击的链接