在UINavigationBar中隐藏或禁用UIBarButtonItem

时间:2013-03-06 02:31:35

标签: ios objective-c xcode uinavigationbar uibarbuttonitem

我正在呈现一个带有模态Segue的UIViewController。 这个UIViewController继承了UINavigationBar,我在导航栏的左侧设置了一个UIBarButtonItem:

HELP ME!!!

我的问题是,当调用所呈现的UIViewController的viewWillAppear方法时,我试图隐藏UIBarButtonItem,但总会出现UIBarButtonItem。 我已经为UIViewController声明了一个名为myCancelButton的UIBarButtonItem,我在Interface Builder中引用了它。

这是我到目前为止所尝试的(所有这些都不起作用):

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    self.navigationItem.leftBarButtonItem = nil;
    self.navigationItem.backBarButtonItem = nil;
    self.myCancelButton = nil;

    NSMutableArray *barButtonItems = [self.navigationItem.leftBarButtonItems mutableCopy];
    [barButtonItems removeObject:self.myCancelButton];
    self.navigationItem.leftBarButtonItems = barButtonItems;

    [self.navigationItem.leftBarButtonItems delete:self.myCancelButton];

    self.navigationController.navigationItem.leftBarButtonItem = nil;
    barButtonItems = [self.navigationController.navigationItem.leftBarButtonItems mutableCopy];
    [barButtonItems removeObject:self.myCancelButton];
    self.navigationController.navigationItem.leftBarButtonItems = barButtonItems;

    [self.navigationController.navigationItem.leftBarButtonItems delete:self.myCancelButton];

    [self.myCancelButton setWidth:0.01];

    self.navigationItem.hidesBackButton = TRUE;

    //Even setting enabled to false doesn't work:
    [self.myCancelButton setEnabled:FALSE];
}

2 个答案:

答案 0 :(得分:1)

这应该有效。

[self.navigationItem setHidesBackButton:YES animated:YES];

答案 1 :(得分:0)

尝试使用此

self.navigationController.navigationItem.leftBarButtonItem = nil;
[self.navigationController.navigationItem setHidesBackButton:YES];