Jean Paul
Jean Paul

Reputation: 2439

Setting the identifier of a UIBarButtonItem programmatically

I have a UIBarButtonItem item in a xib file. I can set its identifier as play, pause, page curl etc in the xib file. Now how can I do that programmatically?

Upvotes: 4

Views: 7174

Answers (4)

thread-game
thread-game

Reputation: 65

I have same issue and I have read wattson12's answer then I solve another similar way. I do not know which is more effective.

//play button

@IBAction func startIt(sender: AnyObject) {

    startThrough();

};

//play button

func startThrough(){ timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true);

    let pauseButton = UIBarButtonItem(barButtonSystemItem: .Pause, target: self, action: "pauseIt");
    self.toolBarIt.items?.removeLast();
    self.toolBarIt.items?.append( pauseButton );

}

func pauseIt() {

    timer.invalidate();

    let play = UIBarButtonItem(barButtonSystemItem: .Play, target: self, action: "startThrough");
    self.toolBarIt.items?.removeLast();
    self.toolBarIt.items?.append( play );

}

Upvotes: 0

Sten
Sten

Reputation: 3854

If you set the Identifier to "Custom" in IB you can at least change the title:

 -(IBAction)editList:(UIBarButtonItem *)sender {
    edit=!edit;
    [imageListTable setEditing:edit animated:NO];
    if(edit){
      [sender setStyle:UIBarButtonItemStyleDone];
      [sender setTitle:@"Done"];
    }else{
      [sender setStyle:UIBarButtonItemStyleBordered];
      [sender setTitle:@"Edit"];
    }
 }

Upvotes: -3

villevenalainen
villevenalainen

Reputation: 11

If I understood you right you want to switch between different system images with your button? I just had a similar case to switch between Edit and Done. Even though those are text labels the situation is pretty much the same.

The only way I managed to do this was to create two separate UIBarButtonItem instances in viewDidLoad in a way wattson12 describes above and assign the right one to self.navigation.leftBarButton when needed.

Upvotes: 1

wattson12
wattson12

Reputation: 11174

this should work (in viewDidLoad)

UIBarButtonItem *barButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:tar action:act] autorelease];
self.navigationItem.rightBarButton = barButtonItem;

where systemItem is the UIBarButtonSystemItem type you want to use. full list of options here

Upvotes: 7

Related Questions