NAvigationController Bar Buttons not showing - objective-c

I have NavigationViewController class and in viewDidload methods
like this. but Bar Buttons not showing any clue?
MapViewController *p=[[MapViewController alloc]initWithNibName:self.nibName bundle:[NSBundle mainBundle]];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]
initWithTitle:#"Code"
style:UIBarButtonItemStylePlain
target:self
action:#selector(showDocco:)];
[[self navigationController] setNavigationBarHidden:NO animated:NO];
self.navigationItem.rightBarButtonItem = rightButton;
self.modalPresentationStyle=UIModalPresentationFormSheet;
[self pushViewController:p animated:YES];

Yiğit, if you want to show the bar button items in MapViewController then your code should be like as follows.
p.navigationItem.rightBarButtonItem = rightButton;
Lets say. you are in HomeViewContrller and you want to push MapViewController on button press.
Your code is setting your bar button item in your HomeViewController becuase you set there self.navigationItem.rightBarButtonItem = rightButton; in viewDidLoad method inside HomeViewController. it will access the navigation item of HomeViewController.
In short, each and every viewcontroller has its own navigation items. Either you should specify the barbutton item with particular viewcontroller reference as i showed you in my answer Or you can transfer your barbutton item code inside MapViewController viewDidLoad method.
Hope this helps

you have given nib name and do you have navigation controller in the nib?If you dont have navigation bar then add one toolbar and add the bar button item to navigaiton bar.

Related

Storyboard: How to set a UIToolbar at the bottom of a UITableViewController controlled by a UINavigationViewController

I'm new to the Cocoa topic. I build with the Storyboard a small application which runs just fine.
I'm pushing UITableViewController to another one, controlled by the UINavigationViewController. Now I need a UIToolbar with a UIBarButtonItem fixed at the bottom of the screen.
Here can you see my Storyboard So I added via drag and drop a Toolbar and in the toolbar an button into the Period TableViewController, thus it is on the same hierarchic level like the UITableView.
Unfortunately neither the "PayOff" Button nor a bottom Toolbar does appear in the app.
Can you help me, what is wrong with my Storyboard?
In your ViewController try adding this
[self.navigationController setToolbarHidden:NO animated:YES];
and use this to add BarItems to it
[self setToolbarItems:#[item1, item2, item3] animated:YES];
I'm using this method to add a scan button to a UITableViewController:
UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
...
...
UIBarButtonItem *scanItem = [[UIBarButtonItem alloc] initWithCustomView:scanButton];
[self setToolbarItems:#[leftSpace, scanItem, rightSpace] animated:YES];
You should be careful because the ToolBar visibility is set for the entire NavigationController you are using and you should show/hide it when needed.
Also the items on it need to be set on each controller (I have this issue, maybe there is a better way to do it)
Hope this helps.

Replacing UITabBar with UIToolbar programmatically

I'm trying to write a tabbed application where each tab is a Navigation Controller. This tab bar appears at every view in the navigation controller as its being inferred on each view.
I would like to replace this tab bar on a detail view with a tool bar with a couple of buttons on it.
I've tried the following code in that detail view's viewDidLoad: method
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
UIBarButtonItem *accept = [[UIBarButtonItem alloc] initWithTitle:#"Accept"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
UIBarButtonItem *decline = [[UIBarButtonItem alloc] initWithTitle:#"Decline"
style:UIBarButtonItemStyleBordered
target:self
action:nil];
NSArray *items = [NSArray arrayWithObjects:accept, decline, nil];
[self.navigationController.toolbar setItems:items animated:YES];
// code suggested
[self.view addSubview:self.navigationController.toolbar];
It still doesn't show up. Though hides the tab bar now for adding the following line in the view that's presenting the detail view:-
theDetailTableViewController.hidesBottomBarWhenPushed = YES;
Have I missed something?
I usually put toolbarHidden = YES or NO, as applicable, in the viewWillAppear or viewDidAppear methods. I am not sure if that is why it is not working for you, but you need to address when you return to the presenting view anyway.
If you don't address it, the toolbar will still be visible when you go back.
Wherever you are pushing your detailViewController from, do this to hide the Tab Bar in the detail view:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.hidesBottomBarWhenPushed = YES;
[self.navigationController detailViewController animated:YES];
and in your detail view, just add the ToolBar as a subview to the detailView.

Back button not appearing on UINavigationController

I have a UINavigationController setup in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the navigation controller's view to the window and display.
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
In my RootViewController I am pushing another view onto the stack:
//Show the deals
DealViewController *dvc = [[DealViewController alloc] initWithNibName:#"DealViewController" bundle:nil];
[self.navigationController.navigationBar setHidden:NO];
[self.navigationController pushViewController:dvc animated:YES];
The view shows up, but there is no back button that is added to my navigation bar. Why is this and how can I resolve it?
Are you setting self.title in RootViewController? Perhaps the UINavigationController doesn't have any text to put on the back button, so it omits it...?
Are you setting hidesBackButton = YES or backBarButtonItem = nil in DealViewController, or does it have a different leftBarButtonItem defined?
Try this:
DetailViewController *detailViewController = [[DetailViewController alloc] init];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle : #"Back"
style : UIBarButtonItemStyleDone
target : nil
action : nil];
self.navigationItem.backBarButtonItem = back;
[self.navigationController pushViewController : detailViewController animated : YES];
[detailViewController release];
You must think of the navigation controller as a stack of navigation controllers each controlling one screen full of information.
You instantiate the navigation controller with the
-(id)initWithRootViewController:(UIViewController *)rootViewController
method. You specify the root view controller in this call. Then you add the navigation controller's view as a subview to the window, like you did before.
If you want to show your second screen you push another view controller on the stack by using
-(void)pushViewController:detailViewController animated:YES
method.
Using presentModalViewController to show the naviagtionController. Set the navagitionController bar button like so:
[navigationController.navigationBar.topItem setLeftBarButtonItem:
[[[UIBarButtonItem alloc] initWithTitle: #"Back"
style: UIBarButtonItemStylePlain
target: self
action: #selector(dismisstheModal:)] autorelease]];
This happened to me because in my navigation controller's content controller I had set up some navigation controller behavior in viewDidLoad and in another class that inherits from my content controller, and the one that was being presented, i implemented a viewDidLoad as well, and forgot to call [super viewDidLoad] which thereby caused me to override the base class's viewDidLoad where I was setting up my navigation controller buttons. Oooops.

How to add a UiBarButtonItem in UINavigationController for all viewControllers

I wonder if it's possible to add a button directly into a navigation controller, or I have to add button from all viewController pushed into navigation Controller?
Example: I have 3 UIViewController (VC1, VC2 and VC3) and I can push this elements in a NavigationController (NC). If I need to add a button as right navigation item for VC1 I can write in viewDidLoad of VC1 :
UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc]initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(settings)];
self.navigationItem.rightBarButtonItem = settingsBtn;
If I need this button on NC also for VC2 and VC3 I have to add this code in VC2 and VC3 too and specify method "settings" in VC1,VC2 and VC3
Is it possible to add this button in a shared way? And how can I define settings method as shared method ?
One way to do this is to have a parent class that your UIViewControllers derive from. In the parent class, you can set the button in, say, viewDidLoad. In your children classes, you would do [super viewDidLoad]; in your viewDidLoad method.
Hope this helps!
Become a delegate of the UINavigationController.
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated {
UIBarButtonItem *settingsBtn = [[UIBarButtonItem alloc]initWithTitle:#"Settings"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(settings)];
viewController.navigationItem.rightBarButtonItem = settingsBtn;
}

UIToolbar in a popover

Is it possible to show toolbar items in a UIViewController inside a popover? I'm doing this in the viewDidLoad method of my view controller:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
[self setToolbarItems:[NSArray arrayWithObject:addButton]];
[addButton release];
Then I'm wrapping this view controller in a UINavigationController (which has a toolbar property, and according to the docs, I'm supposed to use the setToolbarItems method of UIViewController to add items to the toolbar), then presenting it in a popover.
I do not see the toolbar. Are toolbars unsupported when using a popover?
Thanks
Figured it out, apparently the toolbar is hidden by default so you have to do this:
[self.navigationController setToolbarHidden:NO animated:NO];
To make it appear.