Solved

Modify UIToolBarItem image

Posted on 2011-09-30
17
683 Views
Last Modified: 2012-06-21
Hello EE Mac,

I have a UIToolBarItem at the buttom and if the user presses it, it update a value in my DataBase.
What I would like is a new image appears on that same Item so the user knows it has been pressed if he goes back to see the current page.

Actually, it a kind of "Like"   like in Facebook. so when he presses, I would lile to see an image of a Thumbs up. if he presses again, It would be an image of "Like me!"    you know ?

How can I change this without creating a new Item or maybe I could have 2 UITOolbarItem at the same X Y  and one is visible the other is not?

: /
0
Comment
Question by:PhilippeRenaud
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
17 Comments
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36895582
You can use the setImage: method of the UIToolBarItem.

[[myViewController tabBarItem] setImage:[UIImage imageNamed:@"anImage.png"]];

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899488
so I can recall it by the index? when you write tabBarItem you mean the name of it?/ the index of it?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899655
tabBarItem is a property of your view controller.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899668
Ok but I am trying to figure out how do I recall the item I want to change the image?
Lets say I have 4 of them, and the 3rd one on the left is the one i need to change..?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899689
Where do you change the image?  Are you in the viewController that the tab represents?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899696
Yes, I have a @selector on the tabBarItem image  called buttonHit

and in it this is where I would like to change the image.

But I just remembered that i pass  (id) sender     and I guess I could use that?  Not sure how tho..
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899699
Could you post your code please?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899700
So you are simply tapping the tab bar, and it could be in any tab?
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899706
this is how I create the BarButtonItem.

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

little after I do this line:

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



then on the actionSelector:


-(void)likeButtonHit:(id)sender{
change image here....
}
0
 
LVL 13

Accepted Solution

by:
SpeedyApocalypse earned 500 total points
ID: 36899718
With your sender, just set the image.

[sender setImage:[UIImage imageNamed:@"anImage.png"] forState:UIControlStateNormal];

Open in new window


You can also add some code to  see what it currently is.

if ([sender imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"anImage.png"]) {
    [sender setImage:[UIImage imageNamed:@"anotherImage.png"] forState:UIControlStateNormal];
} else {
    [sender setImage:[UIImage imageNamed:@"anImage.png"] forState:UIControlStateNormal];
}

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899725
but will I be able to change the image of the UIImage? because remember in my code i need to create an UIImage and a UIButton to get a colored barButtonItem otherwise its blank.. ?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899729
That code changes the button's image.  You can also iterate through the tabBar toolbarItems array and change the custom view altogether.
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899757
ok . just one last thing.  when I start to write your code, after writing sender i do not see imageForState when I press ESC
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899758
Try typecasting it.  [(UIButton *)sender imageForStat...];
0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36899778
Do you see the error??

    if([(UIButton*)sender imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"240x320-thumbs-up.png"]){
       
    }

expected ) before isEqual

looks fine to me..?
0
 
LVL 13

Expert Comment

by:SpeedyApocalypse
ID: 36899781
My bad.  Typing on the browser is unreliable at best.

if([[(UIButton*)sender imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"240x320-thumbs-up.png"]]){
        
    }

Open in new window

0
 
LVL 1

Author Comment

by:PhilippeRenaud
ID: 36903723
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Plain Text Editor for iPad 6 120
iPad Testing Issue 4 75
iOS Mail App for Exchange Accounts 4 61
How to Add a New Device to Apple Developer Portal 2 37
In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Apple Developer Reference describes NSInvocation this way: An NSInvocation is an Objective-C message rendered static, that is, it is an action tur…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question