E-Madd
E-Madd

Reputation: 4572

Disabling RightBarButtonItems

My viewController is functioning as a container and has its own UINavigationBar. It is not in a naviagtion controller. My nav bar items are set up like so...

self.navigationItem.leftBarButtonItems = leftItems;
self.navigationItem.rightBarButtonItems = @[logout, settings];
[self.navBar setItems:@[self.navigationItem]];

At various points in the application this navigation bar is locked down until the user completes a task. This snippet works fine for toggling the enabled property of the buttons in the navigation bar but only on the leftBarButtonItems! Why?

for(UIBarButtonItem *rightButton in self.navigationItem.rightBarButtonItems){
     [rightButton setEnabled:!rightButton.enabled];
}
for(UIBarButtonItem *leftButton in self.navigationItem.leftBarButtonItems){
     [leftButton setEnabled:!leftButton.enabled];
}

Upvotes: 1

Views: 2914

Answers (1)

Joiningss
Joiningss

Reputation: 1320

UPDATE:

I had created a test demo, it works well. Here is the sreenshots and code, hope to give you some help!

enter image description here enter image description here

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic,strong) UINavigationItem * navItem;
@property(nonatomic,assign) IBOutlet UINavigationBar * navBar;

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem* barItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    UIBarButtonItem* barItem4 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barItemClicked:)];
    self.navItem = [[UINavigationItem alloc] init];
    self.navItem.leftBarButtonItems = @[barItem1,barItem2];
    self.navItem.rightBarButtonItems = @[barItem3,barItem4];
    [self.navBar setItems:@[self.navItem]];

}
- (IBAction)anableSwitch:(id)sender{
    UISegmentedControl * swith = (UISegmentedControl *)sender;

    for(UIBarButtonItem *rightButton in self.navItem.leftBarButtonItems){
        [rightButton setEnabled:(swith.selectedSegmentIndex == 0)];
    }
    for(UIBarButtonItem *leftButton in self.navItem.rightBarButtonItems){
        [leftButton setEnabled:(swith.selectedSegmentIndex == 0)];
    }
}
- (void)barItemClicked:(id)sender{
    NSLog(@"barItemClicked");
}

Upvotes: 8

Related Questions