ios - 如何隐藏/显示导航栏中的右键

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

很遗憾,以下方法不起作用:

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

有办法吗?

最佳答案

通过将引用设置为 nil 来隐藏按钮,但是如果您想稍后恢复它,您需要保留它的副本以便重新分配它。

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

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

就个人而言,在我的应用程序中,我将导航按钮设置为 @properties,以便我可以随意丢弃和重新创建它们,例如:

//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;

https://stackoverflow.com/questions/5588767/

相关文章:

c - Objective C boolean 数组

iphone - 滚动后检测 UITableView 中的当前顶部单元格

ios - Xcode 8 中的单元测试失败

ios - 如何将 NSDate 转换为相对格式 "Today","Yesterday","a we

objective-c - UIAlertController 延迟显示

ios - 为什么 UICollectionView 的 UICollectionViewCell

ios - 显示 UIAlertController 的简单 App Delegate 方法(在 S

iphone - NSLocale 和国家名称

iphone - 改变 setContentOffset 的速度 :animated:?

objective-c - 核心数据 : error: Failed to call designa