简体   繁体   English

条形图按钮项仅在一个标签栏控制器导航栏中

[英]Bar Button Item in only one of the tab bar controller navigation bar

I have a tab bar controller with 4 views controller, and have this tab bar controller in a navigation controller. 我有一个带有4个视图控制器的标签栏控制器,并在导航控制器中有这个标签栏控制器。

I want to display a UIBarButtonItem for just one specific view controller of the tab bar controller. 我想为标签栏控制器的一个特定视图控制器显示一个UIBarButtonItem。

I tried to use the following 我试着使用以下内容

if (tabBarController.selectedViewController == customTourViewController)
    {
        [tabBarController.navigationItem setRightBarButtonItem:done];
    }

But the button doesn't show up. 但按钮没有显示出来。

If I put every view controller in a navigation controller, then the button shows up for only that view, but I end up having 2 navigation bars. 如果我将每个视图控制器放在导航控制器中,则该按钮仅显示该视图,但最终我有2个导航栏。

Is there any way I can implement the first solution? 有什么方法可以实现第一个解决方案吗? Thanks. 谢谢。

In my individual view controllers for the individual tabs, I have the following in the one that needs the button: 在各个选项卡的各个视图控制器中,我在需要按钮的控制器中有以下内容:

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

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                    style:UIBarButtonSystemItemDone target:nil action:nil];
    self.tabBarController.navigationItem.rightBarButtonItem = rightButton;
}

And in the view controllers that don't need the button, I have: 在不需要按钮的视图控制器中,我有:

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

    self.tabBarController.navigationItem.rightBarButtonItem = nil;
}

So, if it's not working for you, I'm not sure if it's your reference to tabBarController without the self designation (if I omit the self I get a compiler error). 所以,如果它不适合你,我不确定它是否是你没有self指定的tabBarController的引用(如果我省略self我得到编译器错误)。 And where is this code because if it's in your tabBarController subclass, then you want self.navigationItem.rightBarButtonItem , right? 这段代码在哪里,因为如果它在你的self.navigationItem.rightBarButtonItem子类中,那么你想要self.navigationItem.rightBarButtonItem ,对吧? Do you have your own ivar defined for that variable name? 您是否为该变量名定义了自己的ivar? Or are you sure that done is defined properly (ie not nil )? 或者你确定done定义是正确的(即不是nil )? Or are you sure this code is being called at all (perhaps set a breakpoint or insert a NSLog and make sure this code is being reached)? 或者您确定正在调用此代码(可能设置断点或插入NSLog并确保到达此代码)?

Alternatively you can implement viewWillDisappear in the same view where you need the button. 或者,您可以在需要按钮的同一视图中实现viewWillDisappear。

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];        
    self.tabBarController.navigationItem.rightBarButtonItem = nil;
}

The accepted answer above is exactly what I needed, just wanted to convert it to Swift for those in the future. 上面接受的答案正是我所需要的,只是想将它转换为Swift以供将来使用。

I've added the code below to the view controller that required the bar button (I've created an add button for this example): 我已将下面的代码添加到需要条形按钮的视图控制器中(我为此示例创建了一个添加按钮):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

self.tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
}

In the view controllers that don't require this bar button, simply add the code below 在不需要此栏按钮的视图控制器中,只需添加以下代码即可

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

self.tabBarController?.navigationItem.rightBarButtonItem = nil
}

You use viewWillAppear vice viewDidAppear because you want the bar button to appear every time the user goes to the designated view controller. 您使用viewWillAppearviewDidAppear因为您希望每次用户转到指定的视图控制器时都会显示条形按钮。

Simply put, viewDidAppear is called once at runtime, viewWillAppear will be called every time the view controller is visited. 简单地说, viewDidAppear在运行时被调用一次,每次访问视图控制器时都会调用viewWillAppear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM