[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 729
  • Last Modified:

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?
0
PhilippeRenaud
Asked:
PhilippeRenaud
  • 8
  • 4
1 Solution
 
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
 
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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
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
 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now