Set IBAction for edit button in NavigationBar - iphone

How to set an IBAction for the edit button in the NavigationBar created using the following code
self.navigationItem.leftBarButtonItem = [self editButtonItem];

Since you are assigning the left bar button programmatically, you must create the button yourself. When you instantiating the button, you can also set the callback action something like this:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit
target:self
action:#selector(editButtonPressed:)];
self.navigationItem.leftBarButtonItem = editButton;
When the edit button is pressed, the following method will be called
- (void)editButtonPressed:(id)sender
{
// do something
}

Since you are already working in code, rather than Interface Builder, it's probably easier to assign a selector programmatically as well:
[self.navigationItem.leftBarButtonItem setAction:#selector(doSomething:)];

Related

Can't add button on navigation bar

I have UItable with items. I have navigation bar etc. On one item in table "Category" I pust another UITable like this:
CategoryTableViewController *b = [[CategoryTableViewController alloc]init];
[self.navigationController pushViewController:b animated:YES];
[b release];
Now I want to add "ADD" button in navigation bar and I add UINavigationBarItem in *.xib connect it to outlets and add it like this in viewDidLoad:
self.navigationItem.rightBarButtonItem = self.addButton;
And this does not work (addButton is null), but when I put the same code for adding button in my first UITable it works fine and "ADD" button is added.
What could be the problem here?
In the ViewController that should show the button in the navigation bar type in the viewDidLoad() method:
self.addToolbarButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Add", nil)
style:UIBarButtonSystemItemAdd
target:self
action:#selector (add)] autorelease];
self.navigationItem.leftBarButtonItem = addToolbarButton;
That will add a "add" styled button to the left in the navigation bar which calls the selector method:
-(void) add {...}
in the same class when it is tapped. In this method in the same class you can specify your add logic. If this method should be placed in a different class, set the target to that.
That is the programatical way to solve this. The method "-(void) add" is what your Outlet has been in the .xib approach.
For your .xib approach you should verify that the Outlet property for the navigationBarButton is set to retain.
self.addButton is NULL, thus make sure its not NULL. Create a button from code.
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneMeasuring:)];
is [super viewDidLoad]; the first call in your viewDidLoad method?
If not try to put it at the very beginning of viewDidLoad.

Receiving UINavBar Items actions

- (void)viewDidLoad
{
//So I created a NavBar Item in my detail view
UIBarButtonItem * saveButton =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveFunc)];
self.navigationItem.rightBarButtonItem = saveButton;
}
I want to write some code to save the data when the user clicks the save button in the NavBar. How do I write the method for this. I am stuck I did it when I created a navBar item through interface builder but I am having troubles doing it when I create the button through code.
now just implement the method
-(void) saveFunc
and dont forget to add:
[saveButton release];

Delegate method called for rightBarButtonItem of UIBarButtonItem

What is the delegate method called when rightBarButtonItem of UIBarButtonItem is tapped? I want to implement a specific action there.
There is no predefined delegate method. You need to set the delegate/action (similar to a UIControl). For example, create the UIBarButtonItem in viewDidLoad the following way:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Mark" style:UIBarButtonItemStylePlain target:self action:#selector(actionForTap:) autorelease];
and implement actionForTap: in your view controller. If you already have the UIBarButtonItem you can set the target/action to those of your desired delegate method, e.g.:
self.navigationItem.rightBarButtonItem.target = self;
self.navigationItem.rightBarButtonItem.action = #selector(actionForTap:);
As a third way, you can configure it in IB (but I'm not going there).
You don't need any delegate method. You can simply use the below code.
This is code for simply adding a button on the right and adding an action to that button.
In my case I called the method named "AddButtonMethod".
UIBarButtonItem *AddButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(AddButtonMethod:)];
self.navigationItem.rightBarButtonItem = AddButton;

Customizing UINavigationController's Edit button?

How do I draw "save" & "cancel" buttons on the navigation bar when the user taps "edit"? Also, how do I hide the delete "knobs" and instead make each row editable, like Apple's Contacts app?
An UIViewController has a -setEditing:animated: method that you can overwrite. In this method you could call -setRightBarButtonItem or -setLeftBarButtonItem.
As for your second question, take a look at the UITableViewDataSource. There's a method called -tableView:moveRowAtIndexPath:toIndexPath.
EDIT: If you want to enter text in a UITableViewCell, you have to place a UITextField inside it in your -tableView:cellForRowAtIndexPath:. Look here.
To create a Cancel button on the left side, implement the setEditing:animated: method and put the following inside.
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelEdit:)] autorelease];
And to add a Save button:
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(SaveEdit:)] autorelease];
Make sure you implement cancelEdit: and saveEdit:.
To both answers, I should add the following:
To remove added button, set the right/leftBarButtonItem to nil

How to change UIBarButtonItem's type in UINaviagationBar at runtime?

I am working on an iPhone's view which composed 3 elements, UITextView, UIToolBar with an UIBarButtonItem.
The goal is, I want UIBarButtonItem change its style from 'edit' (UIBarButtonSystemItemEdit) to 'Done' (UIBarButtonSystemItemDone) and update new selector to new method.
First of all, I have tried following code but it doesn't work:
Could you help me on this idea?
There is a builtin bar button with this behaviour, you get it via the editButtonItem property of a UIViewContoller. Tabbing that button will change the view controller it came from into editing mode, and toggle the button into a done button.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
If you have added the button through IB then make sure to set the identifier to Custom
Also allocate a button in the .h with appropriate IBOutlet and Property
Synthesize the button in .m
Then in your code do the following:
// Set to done
editButton.style = UIBarButtonItemStyleDone;
editButton.title = #"Done";
// Set back to edit
editButton.style = UIBarButtonItemStyleBordered;
editButton.title = #"Edit";
to change the button the Done button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleDone];
to change the button to Edit button use this
[self.navigationItem.rightBarButtonItem setStyle:UIBarButtonItemStyleBordered];
I ended up doing something like this. Unfortunately, setting the title directly did not work, for some reason it was nil and would not let me set it to a different value. The self.editButton comes from an IBOutlet with the target and actions set. This code uses ARC. I hope this helps someone.
NSString *title = app.settings.editing
? NSLocalizedString(#"Done", #"")
: NSLocalizedString(#"Edit", #"");
UIBarButtonItemStyle style = app.settings.editing
? UIBarButtonItemStyleDone
: UIBarButtonItemStyleBordered;
UIBarButtonItem *editButton
= [[UIBarButtonItem alloc] initWithTitle:title
style:style
target:self.editButton.target
action:self.editButton.action];
self.navigationItem.rightBarButtonItem = editButton;