adding button to the navigation bar - iphone

i have a problem while adding button to the navigation bar.. My application consist of two view controllers added to the viewControllers array of a tabBarController. Inturn this tabBarController is added to the viewControllers array of a navigationController. In one of the views i have a textfield for entering dates . On the tap of this textfield the datepicker will pop up. simultaneously i want to display a done button on the navigation bar. how can i do this... i tried using
self.tabBarController.navigationController.navigationItem.rightBarButtonItem = self.doneButton;
But this is not working...
Pls help me out..

Try this code,
UIBarButtonItem *addButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(#"Done", #"")
style:UIBarButtonItemStyleBordered
target:self
action:#selector(DoneButton)] autorelease];
self.navigationItem.rightBarButtonItem = addButton;
Best of luck.

Try with this
UIBarButtonItem* _doneButton;
self.navigationItem.rightBarButtonItem = _doneButton;

use this one:
UIButton *myBtn = [[UIButton alloc]init];
[self.navigationItem.titleView addSubview:myBtn];

Related

When creating a new nav-based iphone app, how do you add a button to the nav bar?

When creating a new nav-based iphone app, how do you add a button to the nav bar?
I've tried editing the rootviewcontroller.xib and adding a nav bar and nav item and bar button item, but it doesn't seem to make any difference.
Is there a simple way of achieving this through IB, or do i have to duck into code somewhere?
Thanks
Unfortunately this requires a little bit of code. Create your UIBarButtonItem in interface builder and hook it up to an IBOutlet in your view controller. Then, in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Assuming "myButton" is the UIBarButtonItem.
self.navigationItem.leftBarButtonItem = myButton;
}
Assigning to rightBarButtonItem works similarly. See the UINavigationItem Class Reference for more information.
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil];

Trying to programmatically add a button to UINavigationController but it never shows up

I've programmatically created some UINavigationControllers and added them to a UITabBarController. Everything seems to work fine but I wanted to add a cancel button to the navigation controller but it never shows up. I've tried multiple ways but I can't seem to affect the display of the navigation items at all and I've followed multiple examples from here and other sites but nothing happens.
MyTableViewController *mtvc = [[MyTableViewController alloc] init];
UINavigationController *myNavController = [[[UINavigationController alloc] initWithRootViewController:mtvc] autorelease];
myNavController.navigationBar.barStyle = UIBarStyleBlackOpaque; // this works
[mtvc release];
// TODO: figure out why added buttons aren't showing
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
myNavController.navigationItem.leftBarButtonItem = closeButton; // never shows up
I also tried adding the button this way
[myNavController.navigationItem setLeftBarButtonItem:closeButton animated:NO]; // also doesn't do anything
I started getting frustrated so I also tried some other things just to see if I could affect anything, but to no avail
myNavController.title = #"test"; // does nothing
I've tried doing it before and after the navigations controllers were added to the UITabBarController and that didn't help.
I've also tried rightBarButtonItem and tried using initWithTitle: instead of initWithBarButtonSystemItem.
Would someone please illuminate me? Clearly, I'm doing this the wrong way.
Try adding the bar buttons in the loadView method of MyTableViewController like the follwing.
UIBarButtonItem *closeButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(shutDown)] autorelease];
self.navigationItem.leftBarButtonItem = closeButton;
I guess that should work.
Have you tried setting the button of the current view controller's navigation item like this:
mtvc.navigationItem.leftBarButtonItem = closeButton;
If you need to do that in Swift 3.0, it is simple like that:
let closeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.cancel,
target: self,
action: #selector({Your-viewController}.{close-method}))
self.navigationItem.leftBarButtonItem = closeButton

Add a button to a navigation toolbar

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.

navigationItem.backBarButtonItem not working? Why is the previous menu still showing as the button?

Trying to customize my back button in a drilldown navigation controller.
On my one view controller I have an Add button where the code programatically generates a new UIViewController:
- (void)add:(id)sender
{
MyAddViewController *addController = [[MyAddViewController alloc] initWithNibName:#"MyAddViewController" bundle:nil];
[self.navigationController pushViewController:addController animated:YES];
[addController release];
}
This works and when I click the add button it drills down into the new view. Inside the viewDidLoad method of MyAddViewController.m I have:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
But this isn't working. The back button in the navigation controller remains the title of the previous view's controller on the stack. It seems that line does nothing. Did I miss something?
Thanks
self.navigationItem.backBarButtonItem is for the back button that appears on the view pushed by the view controller. So you need to move that line to the previous view controller.
This will only work on each child after the viewController that has self.navigationItem.backBarButtonItem.
You're confusing the backBarButtonItem and the leftBarButtonItem. From the UINavigationItem docs on backBarButtonItem:
When this item is the back item of the
navigation bar—when it is the next
item below the top item—it may be
represented as a back button on the
navigation bar. Use this property to
specify the back button. The target
and action of the back bar button item
you set should be nil. The default
value is a bar button item displaying
the navigation item’s title.
So, if you were to change:
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
To:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
I believe you would get the desired effect.
You can't replace the backBarButtonItem, but you can use the leftBarButtonItem to override it. But to get the new button to perform operate the same as the back button, you do need to set the target and action of the new button something like:
- (void)dismissMyView {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:#"Quit" style: UIBarButtonItemStyleBordered
target:self action:#selector(dismissMyView)];
}
If ViewController A push ViewController B meanwhile we want to set the back bar button tittle, we should set "self.navigationItem.backBarButtonItem = ..".if it was set in ViewController B, it will not work as we want.

Back button on UINavigationController

I know that it could seem strange but i need to add a back button on the navigation Bar of the first navigationController's view. I tried like this:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Foo" style:UIBarButtonItemStyleBordered target:self action:#selector(foo:)];
self.navigationItem.backBarButtonItem=backButton;
if instead of backBarButtonItem i write leftBarButtonItem the button is showed. My problem is that i need an arrow button as the normal back button. Is this possible?
Usually this works out of the box, but sometimes with modal views / action sheets you may need this. Just before you instantiate your viewcontroller and push it onto navigationcontroller stack, try
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: #"Back" style: UIBarButtonItemStyleBordered target: nil action: nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
DetailViewController *detailVC = [[DetailViewController alloc]init];
[self.navigationController pushViewController:detailVC animated:YES];
[detailVC release];
I don't think you can do that on the first NavigationController view, because you need to set the backBarButtonItem property in the parent controller, before the child controller is pushed. Also, according the to the Apple docs, the target & action of the backBarButtonItem must be nil.
This question about creating a left-arrow button on a UIToolbar may give you some ideas of how you could work around this using a custom image (for the leftBarButtonItem).
or you could also do the following - I prefer this method. I got this from a different post.
Use following psd that I derived from http://www.teehanlax.com/blog/?p=447
http://www.chrisandtennille.com/pictures/backbutton.psd
Then I just create a custom UIView that I use in the customView property of the toolbar item.
Works well for me.
Hope that helps a little
Of course you can do this. You just need to change the leftBarButtonItem's title to back
then you will get a nice left arrow button with the title back. Then you just change the selector to actually perform a method when the button is clicked. So #selector(foo:)
Here some code on how to achieve the above:
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
self.navigationItem.leftBarButtonItem.title = #"Back";
self.navigationItem.leftBarButtonItem.target = self;
self.navigationItem.leftBarButtonItem.action = #selector(endTextEnteringButtonAction:);
Let me know if that helps.
Apple Document says:
When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item.
So If your navigation item is the top of the Stack (as we are talking here) you can't add the back button to the navigation controller, simply because no place he can navigate back to it because it's the top item in the stack.
Updated Answer :
After I searched I found work a round to make a back button in your root view controller in Navigation controller in these link
It's very simple :)
[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:#"Start" style:UIBarButtonItemStyleBordered target:self action:#selector(initializeStuff)];
self.navigationItem.leftBarButtonItem = backButton;