How to add a navigation controller/bar to iOS? - ios

I am currently trying to add a navigation bar for every screen. However I want to programmatically add different types of buttons available on different screens, specifically on the different slide drawer menu items. Also, I want to be able to have a back button for nested screens.
I have read about the navigation controller and I see there is just a navigation bar that I can add to my xib file, but not sure how to implement it exactly. If I add a navigation bar to the xib file do I have to reference it somehow in my controller or should adding it be enough, because adding this line does not add the items to the navigation bar.
This is the code I add in my MainViewController.m file:
//add navigation top bar items
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = #[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;

Where ever you initially create your MainViewController (and most likely assign it to the windows rootViewController property) wrap this in a UINavigationController.
MainViewController *mainViewController = [MainViewController new];
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
You code in your question is then correct, and those UIBarButtonItems will then show in the navigation bar.

Related

Using the same barButtonItems and toolBarItems for all view controllers

I've created my toolBar and navigationBar and added the respective items to it. I'm still new to coding but its my understanding that UINavigationController only displays the navBar and toolBar for the viewController in the top of the seque. I'm doing this without storyboards and in swift.
If you want to add more than one bar button items to UINavigationBar programatically, you can do that like
UIBarButtonItem *btnShare = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(share)];
UIBarButtonItem *btnRefresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:btnShare, btnRefresh, nil]];
Hope this will help you.

Button not show in the navigation bar

I try this code for the navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
and for the button I tried this code
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
self.navigationItem.rightBarButtonItem=add;
after this code i declare SaveButtonClicked method also.
When I try this code there is no error in this code and compiled successfully but the button is not show in the navigation bar.
Help me with right code and suggestions.
After you have created navigation bar
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0,320, 70)];
Then You need to create navigation item. and add navigation item to navigation bar
UINavigationItem *item = [[UINavigationItem alloc]init];
UIBarButtonItem *add=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(SaveButtonClicked:)];
item.rightBarButtonItem=add;
navbar.items = [NSArray arrayWithObjects:item, nil];
[self.view addSubview:navbar];
Hope this Helps!
1) You don't need to create a UINavigationBar. The navigation controller you're in provides that.
2) And if you did think you needed one, that probably means your view isn't in a navigation controller at all. Check what the value of self.navigationItem is. I imagine you'll find it to be nil. That means you need to redo your interface so this view is actually inside a navigation controller. "Embed in Navigation Controller" under "Editor" in Interface Builder may be of assistance there.
you can just use this single line to achieve that:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveButtonClicked:)];
after this add this method
-(void)saveButtonClicked:(UIBarButtonItem *)sender{
}
for more you can refer Add button to navigationbar programmatically

iOS action of navigation bar back button not working [duplicate]

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.

Adding Back button to UINavigationController inside UITabBarController

The UITabBarController is loaded as a rootViewController from a UINavigationController. I have 6 Tabs and FifthViewController and SixthViewController comes under More tab and they push a DetailsViewController to show details. I am unable to show back button in DetailsViewController to go back to ParentViewController. I tried all the below options but, none of them is working.
I am loading UITabBarController as a initial view controller from Storyboard and have FifthViewController and SixthViewController as well from Storyboard. In FifthViewController I am setting Header
-(void)viewWillAppear:(BOOL)animated
{
self.title = #"Alerts" ;
self.tabBarController.title = #"Alerts" ;
}
In DetailsViewController I am setting Header
-(void)viewWillAppear:(BOOL)animated
{
//I tired all the below options to show back button
}
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.tabBarController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
self.tabBarController.navigationController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil] ;
Where am I doing wrong?
From your description you have it setup incorrectly.
The UITabBarController should be the app's rootViewController. Each tab should be a UINavigationController. Each navigation controller should be setup with the appropriate root view controller for each tab.
The tab bar controller itself should not be in a navigation controller.
You should not be setting the backBarButtonItem of any navigation item.
With the setup I describe, each tab has its own unique navigation. A user can be on tab two, for example, and move forward to the next view controller in that tab. And the tabs will remain visible. Now the user can go to any tab and later return to tab two and still be on the 2nd view controller for that tab.
You need a UINavigationController as the child of the UITabBarController, if you want it to function within the tab with a navigation stack and back button.

Button not being added on navigation controller

- (void)viewDidLoad
{
[super viewDidLoad];
ac = [[AddContacts alloc]init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:ac];
[self.view addSubview:navigationController.view];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
}
Why is my button not being added on the navigation controller?
The UIBarButtonItems are not controlled by the navigation controller, but by each of the view controllers it contains - each UIViewController can have different buttons. Try:
ac = [[AddContacts alloc]init];
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Show" style:UIBarButtonItemStylePlain target:self action:#selector(refreshPropertyList:)];
ac.navigationItem.rightBarButtonItem = anotherButton;
Then initialize the UINavigationController as you have been doing.
There are a few things here that could be causing issues.
Probably what the main issue is this line:
self.navigationController.navigationItem.rightBarButtonItem = anotherButton;
I am pretty sure that what you want to be setting is the right bar button item on the ac view controller's navigation item.
ac.navigationItem.rightBarButtonItem = anotherButton
A few other things though:
Don't have two letter variable names. "ac" is very ambiguous, "addContacts" would provide more information, "addContactsViewController" would provide even more
Are you implementing your own navigationController property in a UIViewController subclass? This is not recommended as it is overriding the navigationController property that UIViewController already has. Give it a different name.
Is the -viewDidLoad method on the parent view controller the place to be assigning the right bar button of your AddContacts object? Consider instead putting the code to set the bar button in the implementation of AddContacts instead.

Resources