 |
 |
|
 |
06-29-2009, 04:29 PM
|
#1 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
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, 05:06 PM
|
#2 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 13
|
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, 05:14 PM
|
#3 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 13
|
Also, won't you be leaking if you don't release the Add button?
|
|
|
06-29-2009, 05:20 PM
|
#4 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
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, 05:25 PM
|
#5 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 13
|
you need a colon after addToFav, since the method signature is - (IBAction)addToFav  id)sender
...action:@selector(addToFav  ];
Hopefully that works.
|
|
|
06-29-2009, 05:29 PM
|
#6 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
That doesn't seem to fix it (woah I'm sloppy w/the code today) heh.
|
|
|
06-29-2009, 05:33 PM
|
#7 (permalink)
|
|
Registered Member
Join Date: Jun 2009
Posts: 13
|
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, 05:43 PM
|
#8 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
Sure thing - I'll keep digging and report back.
|
|
|
06-30-2009, 03: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, 08:08 PM
|
#10 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
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, 02:24 PM
|
#11 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
Still fighting this - bump.
|
|
|
07-02-2009, 01: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 01:21 AM.
|
|
|
07-02-2009, 07:46 PM
|
#13 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
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, 09:38 PM
|
#14 (permalink)
|
|
.38 special tucked
iPhone Dev SDK Supporter
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 132
|
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: 330 |
| 34 members and 296 guests |
| amit.wizkid, ansonl, Batman, carendt242, cavernicola, chorew, cje, ColouredRobot, damienbern, dbonates, donbhar, Duncan C, Ed99, hatembr, Ins3rtNam3H3r3, iSdkDev, iTrackiGiveaway, j0egas, jaime1988, Kalimba, LemonMeringue, Lightsout565, loobian, markdude, mriphoneman, nDev1, Nuncha, rjgrune, Rossco, Sai Baba, smmcneill, vitalys, wheels, yannickd60 |
| Most users ever online was 779, 05-11-2009 at 09:55 AM. |
» Stats |
Members: 24,317
Threads: 39,113
Posts: 171,500
Top Poster: smasher (2,576)
|
| Welcome to our newest member, Lightsout565 |
|