在导航控制器上添加按钮(来自代码)并使它们消失

时间:2012-05-23 11:06:25

标签: objective-c xcode4


我在“视图控制器”上面的代码中添加了一个按钮:

@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");
}

......但我该如何隐藏按钮?

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

self.navigationItem.rightBarButtonItem = nil;

答案 2 :(得分:0)

只需将其设为零

[self.navigationItem setLeftBarButtonItem:nil animated:YES];