add buttons(from code) on the Navigation Controller and make them disappear

。_饼干妹妹 提交于 2019-12-30 10:30:50

问题



I have added a button from the code above "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");
}

...But how can I hide the button?


回答1:


self.navigationItem.rightBarButtonItem = nil;



回答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];



回答3:


Just set it nil

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


来源:https://stackoverflow.com/questions/10718686/add-buttonsfrom-code-on-the-navigation-controller-and-make-them-disappear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!