views:

40

answers:

2

Hi Guys,

I want to disabled the default back button of navigation controller

self.navigationItem.rightBarButtonItem.enabled = NO; self.navigationItem.leftBarButtonItem.enabled = NO;// not working how can we disabled here.

I have done it with manually shown below, But Is there any property to disabled the default back button with just single line?

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

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

A: 

Call [self.navigationItem setHidesBackButton:YES]; for the view controller you do not want to have the back button. Then set the leftBarButtonItem as normal.

jrtc27
Glad I could help.
jrtc27
I want to disabled the button not hiding the button, thanks for your response
Madan Mohan
Why not create an instance of UIBarButtonItem, set it to the leftBarButtonItem, hide the backButton, and set the leftBarButtonItem to your button. Then disable the leftBarButtonItem.
jrtc27
A: 

Using "hidesBackButton=YES" is really not an elegant solution, cause it HIDES the button which is not what we want. An acceptable work-around would be adding a UILabel to the window just over the back button at least disabling the touches on the button.

Add this method to your AppDelegate class:

- (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;
    }
}

You can call it like this from any view controller to disable:

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

To enable:

MyAppDelegate *appDeleg = (MyAppDelegate *) [[UIApplication sharedApplication] delegate];
[appDeleg disableLeftBarButtonItemOnNavbar:NO];
ardalahmet
Thanks for ur response
Madan Mohan
You are welcome.
ardalahmet