Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-29-2009, 05:29 PM   #1 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default 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
mr-sk is offline   Reply With Quote
Old 06-29-2009, 06:06 PM   #2 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 20
Default

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?
Avocado is offline   Reply With Quote
Old 06-29-2009, 06:14 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 20
Default

Also, won't you be leaking if you don't release the Add button?
Avocado is offline   Reply With Quote
Old 06-29-2009, 06:20 PM   #4 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

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.
mr-sk is offline   Reply With Quote
Old 06-29-2009, 06:25 PM   #5 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 20
Default

you need a colon after addToFav, since the method signature is - (IBAction)addToFavid)sender

...action:@selector(addToFav];

Hopefully that works.
Avocado is offline   Reply With Quote
Old 06-29-2009, 06:29 PM   #6 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

That doesn't seem to fix it (woah I'm sloppy w/the code today) heh.
mr-sk is offline   Reply With Quote
Old 06-29-2009, 06:33 PM   #7 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 20
Default

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
Avocado is offline   Reply With Quote
Old 06-29-2009, 06:43 PM   #8 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Sure thing - I'll keep digging and report back.
mr-sk is offline   Reply With Quote
Old 06-30-2009, 04:08 AM   #9 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 39
Default

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.
Artem is offline   Reply With Quote
Old 06-30-2009, 09:08 PM   #10 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

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.
mr-sk is offline   Reply With Quote
Old 07-01-2009, 03:24 PM   #11 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Still fighting this - bump.
mr-sk is offline   Reply With Quote
Old 07-02-2009, 02:07 AM   #12 (permalink)
Registered Member
 
Join Date: Jun 2009
Posts: 39
Default

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.
Artem is offline   Reply With Quote
Old 07-02-2009, 08:46 PM   #13 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Quote:
Originally Posted by Artem View Post
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
mr-sk is offline   Reply With Quote
Old 07-02-2009, 10:38 PM   #14 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

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
mr-sk is offline   Reply With Quote
Reply

Bookmarks

Tags
uibarbuttonitem

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,471
Threads: 89,088
Posts: 380,120
Top Poster: BrianSlick (7,091)
Welcome to our newest member, cherubic
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0