views:

1516

answers:

1

I create a custom right view as follows:

// Build the Segmented Control
NSArray *segmentTextContent = [NSArray arrayWithObjects:[UIImage imageNamed:@"arrow-dice.png"], [UIImage imageNamed:@"arrow-up.png"], [UIImage imageNamed:@"arrow-down.png"], nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];

// Customize the Segmented Control
segmentedControl.momentary = YES;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];

Then I add it to my navigation bar as follows:

// Add the control to the navigation bar right item
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentItem;
self.navigationItem.rightBarButtonItem.title = @"";
[segmentItem release];

I can hide it as follows:

self.navigationItem.rightBarButtonItem.customView.hidden = NO;

QUESTION ...but how can I disable all (or better, a specific element) of the segmented control?

The following does not work.

self.navigationItem.rightBarButtonItem.enabled = NO;

Any ideas appreciated...

Thanks,
matt

+3  A: 

UISegmentedControl.h

- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
- (void)removeAllSegments;
- (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment;       //default is YES

  e.g. [segmentedControl setEnabled:NO forSegmentAtIndex:1];

Hope That helps

Ben Reeves
Thank you, I'd missed that in the docs.Is there an easier way to disable the whole control rather than doing this for each segment in turn?
matt
It's a subclass of UIControl, so you should be able to simply do segmentedControl.enabled = NO;. In the code you presented above, you would add “.customView” after “rightBarButtonItem” and before “.enabled”.
Peter Hosey
That didn't work for me, sadly. Enabled is not a valid property of CustomView.
matt