Neelesh
Neelesh

Reputation: 3693

UIToolBar selected button state

I have a UItableView with a toolbar in the bottom. The toolbar has two buttons say 'fruits','vegetables'. Clicking on them will display only the corresponding category.

I have managed to get the displaying feature working, but i do not know how to set the button in selectedState when chosen.

I created the toolbar as shown below

UIBarButtonItem *fruits = [[UIBarButtonItem alloc] initWithTitle:@"Fruits" style:UIBarButtonItemStyleBordered target:self action:@selector(fruitsClicked:)];
UIBarButtonItem *vegetables = [[UIBarButtonItem alloc] initWithTitle:@"Vegetables" style:UIBarButtonItemStyleBordered target:self action:@selector(vegetablesClicked:)];
NSArray *items = [NSArray arrayWithObjects: fruits,vegetables,nil];
[fruits release];
[vegetables release];
[self setToolBarItems:items];

and selectors like

-(void)fruitsClicked:(id)sender{
     //code here
}

Since i release the buttons earlier, i am not able to set

fruits.enabled=NO;

ANy help would be appreciated.

Upvotes: 1

Views: 3202

Answers (3)

Josh Knauer
Josh Knauer

Reputation: 1754

The sender argument passed to your fruitClicked handler will be a pointer to the UIBarButtonItem. You can cast it like this:

[((UIBarButtonItem*)sender) setEnabled:NO];

However, once the button is disable it no longer accepts taps (i.e. you couldn't tap it again to re-enable it). I would use a customView for the button and implement the enabled/disable appearance yourself.

Upvotes: 0

Erik S
Erik S

Reputation: 1929

I do not have any experience with UIToolBars, but from what i see, the selector gets the ID of the sender, right? You can use that to access the clicked button, and then set it selected.

Upvotes: -1

Ludovic Landry
Ludovic Landry

Reputation: 11774

I think adding separate Buttons to do that is not a good design and strategy, you should use a UISegmentad control which is a component design for what you want: have one segment selected among X.

You can use it like this (to select the first item for example):

UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array];
[segment setSelectedSegmentIndex:0];

And to get the selected item:

segment.selectedSegmentIndex

Upvotes: 2

Related Questions