使用标签栏控制器实现导航栏项目的更简单方法?

Posted

技术标签:

【中文标题】使用标签栏控制器实现导航栏项目的更简单方法?【英文标题】:Easier way to implement navigation bar item with tab bar controller? 【发布时间】:2014-05-30 19:05:52 【问题描述】:

我有一个标签栏控制器作为我的根视图,有 5 个导航控制器(每个标签一个)。每个选项卡中的导航栏将有一个按钮,该按钮在所有选项卡中具有相同的功能。有没有比复制/粘贴到每个导航控制器更简单的方法来添加这个按钮(并响应选择)?

编辑: 在我的自定义导航控制器的子视图控制器中,我有:

- (void)viewDidLoad

    [super viewDidLoad];
    ...
    CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
    [navController addNotificationsButton:YES searchButton:NO];

在自定义导航控制器中我有:

-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search    //Choose which bar button items to add

    NSMutableArray *barItems = [[NSMutableArray alloc]init];

    if (notifications) 
        //Create button and add to array
        UIImage *notificationsImage = [UIImage imageNamed:@"first"];
        UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:@selector(notificationsTap:)];

        [barItems addObject:notificationsButton];
    

    if (search) 
        //Create button and add to array
        UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchTap:)];
        [barItems addObject:searchButton];
    

    [self.navigationItem setRightBarButtonItems:barItems];

但是当子视图控制器加载时,我只是得到一个空的导航栏。有任何想法吗?

编辑 2: 我只需要为要添加按钮的导航栏的视图控制器添加一个参数。这是最终的实现...

对于 CustomViewController.m:

- (void)viewDidLoad
    
        [super viewDidLoad];
        ...
        CustomNavigationController *navController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeNavigationController"];
          [navController addNotificationsButton:YES searchButton:NO forViewController:self];


对于 CustomNavigationController.m

-(void)addNotificationsButton:(BOOL)notifications searchButton:(BOOL)search forViewController:(UIViewController*)vc    //Choose which bar button items to add


    NSMutableArray *barItems = [[NSMutableArray alloc]init];

    if (notifications) 
        //Create button and add to array
        UIImage *notificationsImage = [UIImage imageNamed:@"first"];
        UIBarButtonItem *notificationsButton = [[UIBarButtonItem alloc]initWithImage:notificationsImage landscapeImagePhone:notificationsImage style:UIBarButtonItemStylePlain target:self action:@selector(notificationsTap:)];

        [barItems addObject:notificationsButton];
    

    if (search) 
        //Create button and add to array
        UIBarButtonItem *searchButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchTap:)];
        [barItems addObject:searchButton];
    


      vc.navigationItem.rightBarButtonItems = barItems;


【问题讨论】:

注意:你必须添加@property (strong, nonatomic) CustomNavigationController *navController;到您的 customViewController,否则 ARC 将释放您的 CustomNavigationController,当您点击按钮时会导致 EXC_BAD_ACCESS 【参考方案1】:

您需要创建一个 UINavigationController的子类, 根据需要配置它(使用按钮), 并将其设置为 5 Navigation Controller 的类。

显然我不能在这里写出你所有的应用,但重要的是你有办法

如果您是第一次开发人员,我鼓励您学习 subclassing 等。

【讨论】:

这正是我所需要的,但我还是有点麻烦。你能看看我上面的编辑吗? 在您的故事板中,您是否为每个 navigationController 设置了新的子类? 是的,我将所有 5 个导航控制器的自定义类设置为我的 CustomNavigationController,并为每个控制器提供了不同的故事板/恢复 ID【参考方案2】:

子类导航栏和添加按钮作为子类的一部分。现在您可以使用 5 个子类实例而不是 5 个基本导航栏,并省去添加按钮的重复工作。

【讨论】:

以上是关于使用标签栏控制器实现导航栏项目的更简单方法?的主要内容,如果未能解决你的问题,请参考以下文章

从导航栏视图控制器导航到标签栏视图控制器

将标签栏控制器与导航控制器一起使用时出现问题

所有标签栏控制器的栏按钮

长按而不是简单的轻按

使用带有导航控制器的标签栏应用程序

推送/弹出期间类似消息的更高/标准导航栏