iOS - how to create a navigation bar item in view controller? - iphone

I have been trying to create a navigation bar back button.
Here is my code :-
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;
But it isn't displaying anything on navigation bar.
I am using UIViewController, not an UINavigationController.
Is UINavigationController is the only way to achieve this?
Any help would be really appreciated.
Any link to a good tutorial will be great.
Thanks.

Without the viewcontroller having a navigation controller (i.e viewController.navigationController != nil) you cannot add it in this manner.
One thing you can do is if it is being created by the nib is to just drag a bar button item into a navigation bar and link it via IBAction.

I'd recommend pushing this view controller out of a nvigationcontroller - you will get all those things for free:
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];

If you have your view controller being created from a NIB file in Interface Builder, and the view controller's design surface has a navigation bar on it, the easiest way to do this would be to drag a bar button item from the objects inspector right onto the navigation bar's right side. You would then create an IBAction in your header and implementation that you would hook up to.

I achieve this inserting the button directly in the UINavigationBar:
[yourUINavBar insertSubview:yourButton atIndex:1];

UIViewController's -navigationItem method does only work together with UINavigationController or other UIViewController containments.
You will have to get access to the UINavigationBar and set the item directly.

If you want to have a navigation bar, and have it work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use that UINavigationController where you are using your UIViewController now.
Check out UINavigationController at developer.apple.com for more details.

Related

Navigation Controller - How to Add in Another View Controller in Xcode?

I'm relatively new to iOS programming but I'm learning bit by bit. I've got two nib files, one is my HomeViewController and the other is called 'ReceiptTableViewController'. The HomeVC should not have a top nav bar but the ReceiptTableVC should, with a title and 'back' where the user can swipe to go back to HomeVC.
How would I go about adding this? I've dragged the Navigation Controller to the side of my ReceiptTableVC in the nib file.
I've searched for various answers but some contradict each other as the authors use different versions of Xcode, and some start with storyboards, etc.
Any help is much appreciated!
I haven't used storyboard
You can use this method to decide whether your navigationBar show or not in your viewController.[self.navigationController setNavigationBarHidden: animated:];
In your AppDelegate:
UINavigationController *naviController = [[UINavigationController alloc] initWithRootViewController:homeController];
naviController.navigationBarHidden = YES; //set home controller navigation bar hidden.
self.window.rootViewController = naviController;
Then in your ReceiptTableViewController's viewDidLoad method:
[self.navigationController setNavigationBarHidden:NO animated:NO]; // show the navigation bar.
This is how to declare a UINavigationController programmatically. You can have a try.

how to use toolbar to connect two view controller

have two view controller(FirstViewController,sixViewController),i create a toolbar button in the navigation bar using interface builder,when i press the toolbar button it should direct me to sixViewcontroller,in the sixViewcontroller i have back button to return firstviewController with slide in animation,my toolbar name is Item.help need guys coz I'm still new for iOS.
UIViewController *viewController6 = [[sixViewController alloc]initWithNibName:#"FourthViewController" bundle:nil];
UIViewController *viewController5 = [[FifthViewController alloc]initWithNibName:#"FifthViewController" bundle:nil];
Sounds like you would be much better off using a NavigationController which handles placing the navigation bar and managing the viewController stack automatically.
The UINavigationController class reference is a good place to start
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html

iPhone UINavigationController bar buttons not showing

I am having an issue: I have a tab-bar based app. In MainWindow.xib for the tab that shows the nav controller it links to RootViewController.xib.
in RootViewController.xib I have created a navigation controller and have added a table view.
in RootViewController in viewDidLoad i have [self.view addSubview:navController.view];.
The tableview and navigation controller work well for navigation. pressing a cell pushes the controller, and bar buttons work in the pushed controller.
But when I use self.navController.navigationItem.rightBarButtonItem = barButton; nothing shows up at all. also changing self.navController.navigationItem to self..navigationItem ect.. doesn't help. What do you think the problem could be? I appreciate every answer.
fyi the barbuttonitem is setup with:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Help" style:UIBarButtonItemStylePlain target:self action:#selector(help)];
If u want to use both controller navigation and tab bar controller the u check the sample code of apple documentation. In which both function are use so i given link below.
UIcatalogue
If you init the navigation controller with...
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
Then you can set button with...
rootViewController.navigationItem.rightBarButtonItem = yourButton;

how to add items to the UINavigationBar

I am new to iPhone development.
my application is based on UInavigationBar .
I want to add a navigation bar item in one of my xib, but in the .xib i just simulate the navigation bar so i can't drag and drop the item. thank you
You'll want to add the nav bar buttons programmatically. You see, your xib has a view that is shown within the content view of the UINavigationController. It is the UINavigationBar to which your app has access and which controls the nav bar items. As you point out, your xib has just a placeholder for the nav bar, which is really a convenience for you so your view is sized correctly as you lay it out.
In your UIViewController for the xib, you an add view-appropriate nav bar items with code something like
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"View" style:UIBarButtonItemStylePlain
target:self
action:#selector(viewControls:)] autorelease];
Does that make sense?
In order to be able to add items into a UINavigationBar, you need to first add a UINavigationBar to your view and then add items to it.
You cannot drag and drop items on a simulated navigation bar. A simulated Navigation Bar is just there to make sure you have a correct estimate of the view size available to you if you are adding a Navigation Bar by some other means or from code.
You should be using a UINavigationController for a navigation based hierarchy. That will take care of a lot of the lower details of how to make navigation work as you would like it to. I would also recommend setting that all up programmatically. Here is how you would do that.
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
Then when you want to go to a new view controller (the animated sliding that you normally see), you do this
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
If you want to change the title or the buttons, you do this
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:#"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];

Pushing a UITabBarController with a UINavigationBar onto an existing UINavigationBar

Okay I am working on a group project and what we are trying to do is create a "Results Screen" which has It's Own UINavigation and contains two TabBars in it. We are trying push that ontop of an existing UINavigation. Right now at the moment of this code. We have our main App Delegate and in it we instantiate a beginning navigation and tabbar.
startTabBarController.viewControllers=[NSArray arrayWithObjects:templatesVC,recentJobsVC,profileVC,aboutVC,nil];
startTabBarController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"D2P_Logo2.png"]];
startTabBarController.navigationItem.titleView.backgroundColor = [UIColor clearColor];
UIBarButtonItem *tempRightBarButton = [[UIBarButtonItem alloc] initWithTitle:#"New"
style:UIBarButtonItemStylePlain
target:self
action:#selector(makeJob)];
[startTabBarController.navigationItem setRightBarButtonItem:tempRightBarButton];
[mainNavBar pushViewController:startTabBarController animated:NO];
[window addSubview:mainNavBar.view];
Now what we want to do is add another Tab Bar on top with a segmented navigation controller and a tab bar controller.
At the moment were are trying a UIViewController without a xib to do this. I've tried various methods but all that has come up is a blank screen.
In Terms of Layout of what we have we have a Navigation Controller and on it's stack is a UITabBarController -> UITableViewController -> UITableViewController and we want to add on top of that a UIViewController with a UINavigationController and UITabBarController in that.
Any Advice would greaty be appreciated. Thanks in Advance.
Never mind, I found a solution. Ended up using a ModelViewController and then had some issues with a white bar which I solved by searching around these very forums and found out that I just had to hide the status bar before showing the ModelViewController and showing it after it was loaded.
Thanks anyways though.