禁用backBarButtonItem

时间:2011-08-21 18:21:35

标签: iphone objective-c

我尝试用以下方法禁用导航控制器的后退按钮项目:

self.navigationItem.backBarButtonItem.enabled = NO;

它似乎不适用于backButtonItem,但适用于其他条形按钮项。有没有办法暂时禁用它而不必隐藏它?

2 个答案:

答案 0 :(得分:16)

您应该使用以下内容:

self.navigationItem.hidesBackButton = YES;

希望这应该有用。

答案 1 :(得分:2)

在我的情况下,我不希望完全隐藏后退按钮,我只是想在我的初始化例程完成时非常简短地禁用它。所以这就是我最终做的事情:

// in initWithNibName
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton setTitle:@"Cancel" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
[backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0f]];
[backButton setFrame:CGRectMake(0.0f, 0.0f, 68.0f, 28.0f)];
[backButton setEnabled:NO];
[backButton addTarget:self action:@selector(tappedClose) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];

然后,在初始化例程完成后,我这样做:

UIButton *backNavigationButton = (UIButton *)self.navigationItem.leftBarButtonItem.customView;
[backNavigationButton setEnabled:YES];

稍后我计划从使用UIButtonTypeRoundedRect更改为UIButtonTypeCustom并提供我自己的“后退按钮式”按钮图像。希望这有帮助!