如何隐藏/显示导航栏中的右键 [英] How do I hide/show the right button in the Navigation Bar

查看:91
本文介绍了如何隐藏/显示导航栏中的右键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要隐藏导航栏中的右键,然后在用户选择某些选项后取消隐藏它.

I need to hide the right button in the Navigation Bar, then unhide it after the user selects some options.

不幸的是,以下操作无效:

Unfortunately, the following doesn't work:

NO GOOD: self.navigationItem.rightBarButtonItem.hidden = YES;  // FOO CODE

有办法吗?

推荐答案

通过将引用设置为nil来隐藏按钮,但是,如果以后要恢复它,则需要挂在它的副本上,这样您才能重新分配.

Hide the button by setting the reference to nil, however if you want to restore it later, you'll need to hang onto a copy of it so you can reassign it.

UIBarButtonItem *oldButton = self.navigationItem.rightBarButtonItem;
[oldButton retain];
self.navigationItem.rightBarButtonItem = nil;

//... later
self.navigationItem.rightBarButtonItem = oldButton;
[oldButton release];

个人而言,在我的应用中,我将导航按钮设置为@properties,以便可以删除&随意重新创建它们,就像这样:

Personally, in my apps I make my nav buttons into @properties, so that I can trash & recreate them at will, so something like:

//mycontroller.h
UIBarButtonItem *rightNavButton;
@property (nonatomic, retain) UIBarButtonItem *rightNavButton;

//mycontroller.m
@synthesize rightNavButton;
- (UIBarButtonItem *)rightNavButton {
    if (!rightNavButton) {
        rightNavButton = [[UIBarButtonItem alloc] init];
        //configure the button here
    }
    return rightNavButton;
}

//later, in your code to show/hide the button:
self.navigationItem.rightBarButtonItem = self.rightNavButton;

这篇关于如何隐藏/显示导航栏中的右键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆