objective-c - 在导航 Controller 上添加按钮(来自代码)并使它们消失

标签 objective-c xcode4


我在“ View Controller ”上方的代码中添加了一个按钮:

@implementation HBViewController
.....
.....
.....

- (void)viewDidLoad
{
    [super viewDidLoad];
    okButton = [[UIBarButtonItem alloc] initWithTitle:@"Ok" style:UIBarButtonItemStyleBordered target:self action:@selector(okayButtonPressed)];

    [self.navigationItem setRightBarButtonItem:okButton animated:NO];

}

- (void) okayButtonPressed{
NSLog(@"you pressed ok");
}

...但是我怎样才能隐藏按钮呢?

最佳答案

            //to disable

        self.navigationItem.rightBarButtonItem.enabled = NO;

            //to hide - hide means setting nil will remove that button

        self.navigationItem.rightBarButtonItem = nil;


            //if u want to show again then create and assign new button again

        okButton = [[UIBarButtonItem alloc] initWithTitle:@"Ok" 

                  style:UIBarButtonItemStyleBordered

                  target:self action:@selector(okayButtonPressed)];

        [self.navigationItem setRightBarButtonItem:okButton animated:NO];

关于objective-c - 在导航 Controller 上添加按钮(来自代码)并使它们消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10718686/

相关文章:

ios - 创建要在 iPhone 配置实用程序中使用的构建文件(.app 和 .mobileprovision)

objective-c - 如何在垃圾收集的 Obj-C 中保留窗口而不保留指向它的指针?

objective-c - xcode 有错误线程 1 :Program received signal 'SIGABRT'

iphone - 代码 : How to change all fonts at once in an app?

ios - 无法更改 XIB 中的 View 大小?

ios - Xcode 4 中 GHUnit + Hudson/Jenkins 的问题

iOS:如何让我的 block 在不相互干扰的情况下执行?

objective-c - NSDatePicker 使用 Stepper 更改日期

ios - 为什么我会收到构建错误(当我尝试调用它时使用未声明的标识符 'methodName'?

iphone - 更改 UIButton 不同状态的渐变