- (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];
Related
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:)];
I want to make a done button appear UINavigationBar when any row in my UITableView is selected and I want this button to perform an action performSegueWithIdentifier.
Any ideas on how to implement it?
Add the following to your tableView:didSelectRowAtIndexPath: method:
//add done button to navigation bar
UIBarButtonItem *doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(userPressedDone)];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;
Then have a method like this somewhere in your view controller:
-(void)userPressedDone {
//perform segue
[self performSegueWithIdentifier:#"mySegue" sender:nil];
}
I would imagine that in your -didSelectRowAtIndexPath: method you would add a UIBarButtonItem to the right or left bar button item of the navigationItem of your view controller.
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.
How can a button be added to a navigation toolbar in an application which is navigation based?
The rootController is a tableView by default, and this is perfect. I would like to add a "share" button to the navigation controller so that I can attach it to my own method.
How is this done if the navigation bar is added in the code somewhere?
To do it in code, go to your views viewDidLoad method and create a UIBarButtonItem
This code will create a button that says share like you wanted and put it on the right hand side of the navigation bar.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStyleBordered target:self action:#selector(share)];
self.navigationItem.rightBarButtonItem = shareButton;
[shareButton release];
}
UIBarButtonItem *shareButton = [[[UIBarButtonItem alloc] initWithTitle:#"Share" style:UIBarButtonItemStylePlain target:self action:#selector(yourShareAction)] autorelease];
self.navigationItem.rightBarButtonItem = shareButton;
This makes an add button, but you get the idea.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:
self action:#selector(buttonPressed:)]
autorelease];
You can create an UIBarButtonItem and attach it to a root view controller's navigation item. You add a target-action pair when you initialize the UIBarButtonItem object and that action message will be sent to the target on a user tapping on the button.
So if you want the button when the table view is shown, set the UIBarButtonItem to your table view controller's navigationItem.rightBarButtomItem property.
I need to change color on the default "self.navigationItem.backBarButtonItem". To accomplish this I´ve created an custom Button class and implemented it like this:
- (void)viewDidLoad {
[super viewDidLoad];
BackButton *blueSaveButton = [[BackButton alloc] init];
UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:blueSaveButton];
temporaryBarButtonItem.title = #"Tillbaka";
self.navigationItem.backBarButtonItem = temporaryBarButtonItem;
[temporaryBarButtonItem release];
[blueSaveButton release];
[self gotoLocation];
}
But this has no effect at all on the button.
So how do you manage to do this without "breaking" the default/inherited behavior of the navigationbar?
EDIT: The reason is that the button needs to be corporate branded, so default styles will not do.
BR
If you want the button to be blue (like a Done button) and have the title "Save" you can use one of the built in bar button item types:
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(myAction:)] autorelease];
Ok found the solution (not so good but it works) do this:
1: Implement the button from this tutorial: www.switchonthecode.com
This will override the default behavior of navigationbar.
2: Implement this in every view that needs the custom button:
osmorphis.blogspot.com
BR