Search code examples
iphoneobjective-cxcodeinterface-builderuitoolbar

Modify UIToolBar items programmatically


I have created a toolbar using Interface Builder. I have a number of buttons in the toolbar that are hooked up to various IBAction methods. What I want to do is, when selecting one item, visually grey out another by changing it's tint.

How can I get a reference to the BarButtonItem? I can't see where I can give each label a unique name where so that I can reference it specifically.

Update

I created an IBOutlet for the tool bar so i now have acces to it. Unfortunately when I try to set the tintColor it crashes. Here is my code:

UIBarButtonItem *penButton = [_toolBar.items objectAtIndex:3];
UIBarButtonItem *crossButton = [_toolBar.items objectAtIndex:4];

penButton.tintColor = [UIColor darkGrayColor];
crossButton.tintColor = [UIColor whiteColor];

The error thrown:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem setTintColor:]: unrecognized selector sent to instance 0x631df50'


Solution

  • Try this:

    NSArray *items = [myUIToolbar items];
    for (UIBarButtonItem *barButton in items) {
       //do something with button
    }