06-29-2009, 05:29 PM
#1 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Changing UIBarButtonItem Identifier programatically
Hello,
So, I've searched around and looked through the class reference, but I cannot find the answer. I'm trying to change a UIBarButtonItem's Identifier, from 'Add' (+) to Undo once selected.
Code:
- (IBAction)addToFav:(id)sender
{
NSLog(@"Add Button Pressed");
addButton.enabled = FALSE;
//addButton.style = UIBarButtonSystemItemUndo;
UIBarButtonItem *newUndoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(id)];
self.navigationItem.rightBarButtonItem = newUndoButton;
Disabling the button works fine, but what I really want to do is replace it with a Undo button. The above code doesn't work ...
How should I do this?
Thanks in advance -
- sk
06-29-2009, 06:06 PM
#2 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 20
I'm by no means a guru at iPhone development yet, but "action:@selector(id)" looks troublesome to me.
Don't you need to put a method signature there?
Maybe give that a whirl?
06-29-2009, 06:14 PM
#3 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 20
Also, won't you be leaking if you don't release the Add button?
06-29-2009, 06:20 PM
#4 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Avocado - Hey, yeah, I wasn't sure what to put for the @selector ... so, say I just do this instead:
Code:
UIBarButtonItem *newUndoButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(addToFav)];
Still doesn't work ..
As for the leak, yeah I should release in dealloc.
06-29-2009, 06:25 PM
#5 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 20
you need a colon after addToFav, since the method signature is - (IBAction)addToFav
id)sender
...action:@selector(addToFav
];
Hopefully that works.
06-29-2009, 06:29 PM
#6 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
That doesn't seem to fix it (woah I'm sloppy w/the code today) heh.
06-29-2009, 06:33 PM
#7 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 20
Damn...I thought that would do it.
When you figure it out, would you mind posting here? I'd be interested to see what was wrong.
Thanks
06-29-2009, 06:43 PM
#8 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Sure thing - I'll keep digging and report back.
06-30-2009, 04:08 AM
#9 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 39
It works for me:
Code:
[self.navigationItem setRightBarButtonItem:currentRightBarButton];
Also, I noticed that sometimes rightBarButtonItem doesn't get changed for some reason. So I added:
Code:
[self.navigationItem setRightBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:currentRightBarButton];
[self.rootController.tabBarController.view setNeedsDisplay];
This works well.
06-30-2009, 09:08 PM
#10 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Hey Artem & Avocado -
Probably the fact I'm still learning that I'm totally making this a bigger problem than it should be here:
Here's the method that I'm trying to change the UIBarButton in:
Code:
- (IBAction)addToFav:(id)sender
{
NSLog(@"Add Button Pressed");
addButton.enabled = FALSE;
....HERE....
[self insertIntoDB:[NSString stringWithFormat:@"..."]];
}
Currently, I'm just setting the add button to disabled. But what I want to do is make it (create in its place, etc) an 'Undo' button. If they select the 'Undo' button, it should then go back to an 'Add' button.
Also, here's a picture of the controller I'm working with:
http://mr-sk.com/iphone/controller.png
Thanks - much appreciated.
- sk
**EDIT** Artem, I tried what you mentioned, but could not get it to work. I think its an issue with me being confused on exactly the syntax/names of the controllers VS what you've provided...any assistance is greatly appreciated.
07-01-2009, 03:24 PM
#11 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Still fighting this - bump.
07-02-2009, 02:07 AM
#12 (permalink )
Registered Member
Join Date: Jun 2009
Posts: 39
Quote:
Artem, I tried what you mentioned, but could not get it to work. I think its an issue with me being confused on exactly the syntax/names of the controllers VS what you've provided...any assistance is greatly appreciated.
Yeah, sorry for confusion. You need to do something like this:
Code:
[self.navigationItem setRightBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:currentRightBarButton];
EITHER::[self.view setNeedsDisplay];
OR:::::::[self.tabBarController.view setNeedsDisplay]
..'self' here is navigation controller instance.
Last edited by Artem; 07-02-2009 at 02:21 AM .
07-02-2009, 08:46 PM
#13 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Quote:
Originally Posted by
Artem
Yeah, sorry for confusion. You need to do something like this:
Code:
[self.navigationItem setRightBarButtonItem:nil];
[self.navigationItem setRightBarButtonItem:currentRightBarButton];
EITHER::[self.view setNeedsDisplay];
OR:::::::[self.tabBarController.view setNeedsDisplay]
..'self' here is navigation controller instance.
Hey, ok, but I dont have a navigation controller. Checkout this image so you can see my IB/nib structure:
http://mr-sk.com/iphone/controller.png
What's highlighted in the screen shot is the item I'm trying to change. The code you provided doesn't work on what I have.
Thanks for the help this far - I think we're close.
- sk
07-02-2009, 10:38 PM
#14 (permalink )
.38 special tucked
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
So, I kinda got this working - Not totally happy with it, but it works.
I set the UIBarButtonItem to 'Custom', so that allows me to change the title programmatically (as I understand it):
Code:
if ([addButton.title isEqualToString:@"Remove"])
{
addButton.title = @"Add";
}
else
{
addButton.title = @"Remove";
}
That works fine. Still doesn't solve my original problem, but at least I can now move onto something else.
Thanks for the help.
- sk
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 778
26 members and 752 guests
AppAnnex , astalavista , bbbs77 , bellissimo , cherubic , chiataytuday , Dattee , donlucacorleone , FgFactory , jonmoore12370 , ketaskin , kukat , MarkC , marshusensei , OgreSwamp , PixelEnvision , PsychoChris , qkstrk , RobbieKo , Shoopy , sneaky , stevejohnson01 , Tambourin , temi , Thompson22 , twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,471
Threads: 89,088
Posts: 380,120
Top Poster: BrianSlick (7,091)
Welcome to our newest member, cherubic