从TabBarController的导航控制器中隐藏BarButton项

时间:2014-04-01 10:37:29

标签: ios objective-c uinavigationcontroller uitabbarcontroller uibarbuttonitem

我有一个包含三个标签的tabBarController。我想只在第一个ViewController中将rightBarButtonItem显示到导航栏,但是当我回来时我希望它再次可见。怎么做到这一点?

我尝试为BarButtonItem设置enabled = NO,但结果不好。

当视图发生变化时,我也尝试将BarButtonItem设置为nil但是当我回来时已经消失了。

我将barButtonItem从viewDidLoad移到了viewWillAppear,但它无法正常工作。

这是rightBarButtonItem到第一个ViewController的viewDidLoad的代码。

UIImage *refreshButtonImage = [UIImage imageNamed:@"9B_Info.png"];
UIImage *refreshButtonImageB = [UIImage imageNamed:@"9A_Info.png"];

UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];

[refreshButton setImage:refreshButtonImage
               forState:UIControlStateNormal];

[refreshButton setImage:refreshButtonImageB
               forState:UIControlStateHighlighted];

[refreshButton setImage:refreshButtonImageB
               forState:UIControlStateSelected];

refreshButton.frame = CGRectMake(0, 0, 30, 30);

[refreshButton addTarget:self
                  action:@selector(showImageInfo)
        forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *refreshBarButtonItem = [[UIBarButtonItem alloc]  initWithCustomView:refreshButton];

self.refreshBtn=refreshBarButtonItem;

[self.tabBarController.navigationItem setRightBarButtonItem:self.refreshBtn];

3 个答案:

答案 0 :(得分:1)

您是否以编程方式使用故事板或全部?试试这个:

1-打开新项目(单一视图)

2-进入故事板 - >添加导航控制器 enter image description here

3-删除表视图控制器 enter image description here

4-将导航控制器设置为启动控制器(拖动将视图控制器指向导航控制器的箭头) enter image description here

5-将视图控制器设置为导航控制器的根视图控制器 enter image description here enter image description here enter image description here

6-完成您的代码和@ Apurv的解决方案

答案 1 :(得分:0)

替换以下行:

[self.tabBarController.navigationItem setRightBarButtonItem:self.refreshBtn];

这一行:

[self.navigationItem setRightBarButtonItem:self.refreshBtn];

如果只想为viewController显示它,请不要将它添加到tabBarController。

答案 2 :(得分:0)

如果有人在将来提出这个问题,这对我有用:

在第一个视图控制器中,我将其添加到viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];

    NSLog(@"!!!!");

    UIImage *refreshButtonImage = [UIImage imageNamed:@"9B_Info.png"];
    UIImage *refreshButtonImageB = [UIImage imageNamed:@"9A_Info.png"];

    UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [refreshButton setImage:refreshButtonImage
                forState:UIControlStateNormal];

    [refreshButton setImage:refreshButtonImageB
                forState:UIControlStateHighlighted];

    [refreshButton setImage:refreshButtonImageB
                forState:UIControlStateSelected];

    refreshButton.frame = CGRectMake(0, 0, 30, 30);

    [refreshButton addTarget:self
                   action:@selector(showImageInfo)
         forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem *refreshBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:refreshButton];

    self.refreshBtn=refreshBarButtonItem;

    [self.tabBarController.navigationItem setRightBarButtonItem:self.refreshBtn];



}

在viewWillAppear方法的第二个View Controller中:

    self.tabBarController.navigationItem.rightBarButtonItem=nil;

问题是每次viewController进入TabBarController的viewDidLoad只调用一次,但每次viewController出现时都会调用viewWillAppear。