0

I have an edit rightbarbuttonitem in my view in navigation bar. I set it up with the help of storyboard/IB, not programmatically. Now, all i want is to assign an action when the "done" barbuttonitem is pressed (not edit).

Is there a way to achieve it? I tried manually through -(IBAction), but it's not working. Also, i want to perform the action on selected items in UITableView. So if you give me an idea, it would be great.

Monis Manzoor
  • 183
  • 3
  • 14

2 Answers2

3

That button calls the method

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

You can implement it and it will get called everytime your edit/done button gets tapped. All you have to do is check the button's title property to see when it's showing done and when it's showing edit

Andrei Filip
  • 1,145
  • 15
  • 33
0

If you declared your button as an IBOutlet then all you'd need to do is use the synthesised variable on your .m as so:

_yourBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(runMethod)];
self.navigationItem.rightBarButtonItem = _yourBarButton;

Then you'd have to declare your run method:

-(void)runMethod
{
    //do stuff
}
Suiz Uzcategui
  • 145
  • 1
  • 8
  • What if i didn't declare? Is it necessary to declare it? I tried the similar code without IB0outlet and it didn't workout. – Monis Manzoor Sep 02 '12 at 15:07
  • Not necessarily. But then you'd have to write UIBarButtonItem *yourBarButton as your declaration in your .m – Suiz Uzcategui Sep 02 '12 at 15:09
  • I did the same. But it creates another button. I want to overwrite the same on "EDIT" barbuttonitem when it changes to "done". – Monis Manzoor Sep 02 '12 at 15:17
  • Yeah, I'm just showing you how I'd do it. I seldomly use Interface Builder anymore. Just delete the button from your Interface Builder and the one you create from this code will be the one you'd use. :) – Suiz Uzcategui Sep 02 '12 at 15:29
  • without that, i wouldn't be able to select tableview cells, right? which is a problem here. – Monis Manzoor Sep 02 '12 at 15:31
  • You should be able to access the tableview cells either way. Not sure how though, you'd have to google that bit... – Suiz Uzcategui Sep 02 '12 at 15:36
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16158/discussion-between-monis-manzoor-and-suiz-uzcategui) – Monis Manzoor Sep 02 '12 at 15:37