5

我想禁用导航控制器的默认后退按钮

self.navigationItem.rightBarButtonItem.enabled = NO; 
// Below code does not work since leftBarButtonItem is always nil.
self.navigationItem.leftBarButtonItem.enabled = NO;

我已经手动完成了下面显示的操作,但是是否有任何属性可以仅通过单行禁用默认的后退按钮?

backButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 100, 30)];
[backButton setBackgroundImage:[UIImage imageNamed:@"backbutton_100.png"] forState:UIControlStateNormal];
[backButton addTarget:self  action:@selector(backAction:)  forControlEvents:UIControlEventTouchUpInside];
[backButton setTitle:@"  All Customers" forState:UIControlStateNormal];
backButton.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[buttonView addSubview:backButton];

UIBarButtonItem* leftButton = [[UIBarButtonItem alloc] initWithCustomView:buttonView];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];

// Now it is working.
self.navigationItem.leftBarButtonItem.enabled = NO;
4

4 回答 4

11

它很容易.....试试这个

self.navigationController.navigationBar.userInteractionEnabled = NO;   //for  disabling 

self.navigationController.navigationBar.userInteractionEnabled = YES; //for again enabling
于 2011-06-03T12:52:17.873 回答
5

使用“hidesBackButton=YES”确实不是一个优雅的解决方案,因为它隐藏了我们不想要的按钮。一个可接受的解决方法是在窗口中添加一个 UILabel 到后退按钮上方,至少禁用按钮上的触摸。

将此方法添加到您的 AppDelegate 类:

- (void) disableLeftBarButtonItemOnNavbar:(BOOL)disable
{
    static UILabel *l = nil;

    if (disable) {
        if (l != nil)
            return;
        l = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 160, 44)];
        l.backgroundColor = [UIColor clearColor];
        l.userInteractionEnabled = YES;
        [self.window addSubview:l];
    }
    else {
        if (l == nil)
            return;
        [l removeFromSuperview];
        [l release];
        l = nil;
    }
}

您可以从任何视图控制器中这样调用它来禁用:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:YES];

启用:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];
于 2010-07-31T12:35:12.487 回答
2

调用[self.navigationItem setHidesBackButton:YES];您不希望有后退按钮的视图控制器。然后设置leftBarButtonItem为正常。

于 2010-07-08T09:24:06.687 回答
1

你也可以使用

[[_navigationController.topViewController.navigationItem leftBarButtonItem] setEnabled:NO]; // The top view controller on the stack.
[[_navigationController.visibleViewController.navigationItem leftBarButtonItem] setEnabled:NO];// Return modal view controller if it exists. Otherwise the top view controller.

当您想从 Appdelegate 或任何其他视图控制器禁用或启用 UIViewControler 时,您可以使用它。

于 2012-10-17T05:27:37.993 回答