Retrieve UIToolBarItem

Hello EE Mac,

Refering this question for my new question.

http://www.experts-exchange.com/Apple/Programming/Q_27374242.html

I need to modify a uiToolBarItem but this time is when I touch another toolBarItem. In other words, I have Left and right arrows that swap images and at each images I need to update the image of a thumb up or a thumb down depending on the database is the Thumb saved at bool 0 or 1  (1 is up  0 is down)

So I dont have the sender of the thumb barItem
how can I recall the thumbItem for another toolbarItem press?
LVL 1
PhilippeRenaudAsked:
Who is Participating?
 
mad_macCommented:
try this

UIButton *tmpButton = (UIButton*)_likeMe.customView;
if (a == @"0"){
    [tmpButton setImage:[UIImage imageNamed:@"240x320-thumbs-up.png"] forState:UIControlStateNormal];
} else {
    [tmpButton setImage:[UIImage imageNamed:@"320x480-thumbs-down.png"] forState:UIControlStateNormal];   
}

Open in new window


remember the UIButton is contained in the customView property of the UIBarButtonItem, the UIBarButtonItem does not become a UIButton...
0
 
mad_macCommented:
just keep a reference to you UIBarButtonItem in your ViewController, if your building the view as a NIB then simply define an outlet as a property in the code and link it to the item.  your building the view in code then create a property and assign it.

once you have done this you can manipulate the that bar item whenever you want.
0
 
PhilippeRenaudAuthor Commented:
I am building everything in code behind, does it make a difference?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
mad_macCommented:
no not at all, just create a property in the view controller class and assign the UIBarButtonItem to it.  the when you get the message from the other button simply use the property and change the image.
0
 
PhilippeRenaudAuthor Commented:
Ok so I create a UIBarbuttonItem property IBOutlet

I was going to say I assign my draging the line on design view but I cant do that, I am not sure what you mean by Assign ?
0
 
PhilippeRenaudAuthor Commented:
I assign by draging*
0
 
PhilippeRenaudAuthor Commented:
Here's what I do, I am having an error (I think I know why, but not sure on how to do everything right) :

    UIBarButtonItem *_likeMe;
@property(nonatomic, retain) UIBarButtonItem *likeMe;


on .m   :


    if (a == @"0"){
        [(UIButton*)_likeMe setImage:[UIImage imageNamed:@"240x320-thumbs-up.png"] forState:UIControlStateNormal];
    } else {
        [(UIButton*)_likeMe setImage:[UIImage imageNamed:@"320x480-thumbs-down.png"] forState:UIControlStateNormal];  
    }

this is causing error : -[UIBarButtonItem setImage:forState:]: unrecognized selector sent to instance 0x7181300
2011-10-04 12:50:43.518 Fashion 8[514:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIBarButtonItem setImage:forState:]: unrecognized selector sent to instance 0x7181300'


I understand that _likeMe is a UIBarButtonItem and not a UIButton but the problem is that my buttonItem _likeMe has a UIImage and a UIButton because otherwise I couldnt put a custom colored image on it.

any idea?
0
 
mad_macCommented:
humm, can you post the code where you are creating the UIBarButtonItem and add it to the toolbar.
0
 
PhilippeRenaudAuthor Commented:
   UIImage *likeImage = [UIImage imageNamed:@"240x320-thumbs-up.png"];
    UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [likeButton setImage:likeImage forState:UIControlStateNormal];
    [likeButton addTarget:self action:@selector(likeButtonHit:) forControlEvents:UIControlEventTouchDown];
   
    likeButton.frame = CGRectMake(0, 0, likeImage.size.width, likeImage.size.height);
                                 
    UIBarButtonItem *like = [[UIBarButtonItem alloc] initWithCustomView:likeButton];

....

            [self setToolbarItems:[NSArray arrayWithObjects:fixedLeft, flex, left, fixedCenter, right, flex, like, action, nil]];

_likeMe=like;

[like release];
0
 
PhilippeRenaudAuthor Commented:
Ok I tried, I have no error but the image does not change...

and my a variable is fine in NSLog  sometimes its  "0"  sometimes its "1"

mmm....
0
 
PhilippeRenaudAuthor Commented:
Wait a second... I just saw it alwasy goes in the ELSE Statement..


looks like its doesnt like the a == @"0"

im used to .NET  lol
0
 
PhilippeRenaudAuthor Commented:
Thanks, I used  equalToString for my other problem.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.