18

有谁知道如何隐藏 arightBarButtonItem的 a UINavigationControllerrightBarButtonItem在我的应用程序中,我有一个编辑按钮作为UINavigationController. 我想隐藏这个?完成某些操作时的 UIBarButton`。

4

3 回答 3

19

To Hide the right button: self.navigationItem.rightBarButtonItem = nil;

Now, to show it:

  1. If you setup the right button in your view controller by assigning it to self.editButtonItem then simply assign it again in order to show it:

    self.navigationItem.rightBarButtonItem = self.editButtonItem;

  2. If you setup the right button in your view controller by allocating and initing a UIBarButtonItem, then simply keep a reference to the UIBarButtonItem in your view controller, and assign it again when you need to show it.

于 2011-11-12T17:44:19.147 回答
18

尝试

self.navigationItem.rightBarButtonItem = nil;

当你想要它回来时,你将不得不实例化一个按钮,即

UIBarButtonItem *rightBarButton = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch
                                               target:self
                                               action:@selector(searchBar:)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
于 2010-06-15T06:06:31.557 回答
15

如果您需要根据某些条件隐藏/显示按钮,请尝试以下操作:

if (condition) { 
    self.navigationItem.rightBarButtonItem.title = @"";
    self.navigationItem.rightBarButtonItem.enabled = NO;
} else {
    self.navigationItem.rightBarButtonItem.title = @"my button title";
    self.navigationItem.rightBarButtonItem.enabled = YES;
}

这样您就不必在属性中保存对按钮的引用,也不必担心在新按钮上连接操作。

于 2014-03-24T18:03:22.723 回答