How do I add a UINavigationController to a view in code? - iphone

view1 = [[View1 alloc] init]; //Create the first view
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:view1];
navigationController1.navigationBar.tintColor =[UIColor blackColor];
View1 is inherit from UIViewController. So I create a *view1, then I create a UINavigationController, call *navigationController1. How do I link the two together? Thank you very much

The way to link a view controller with a navigation controller is to push the view controller onto the navigation stack. For example:
UIViewController * yourViewController = [[UIViewController alloc] init];
UINavigationController * navigation = [[UINavigationController alloc] init];
[navigation pushViewController:yourViewController animated:NO];
[yourViewController release]
Finally release the view controller at the end since the navigation controller retains it.

You may have things a little mixed up. A UINavigationController is generally attached to a UIViewController, which itself is what contains the UIView.
Before writing your own code, you might take a look at the navigation controller sample application project that is available from Xcode's new project template list, to figure out how it works.

The answer for this question is here: Having problem with pushViewController!! Help

Related

How to implement a navigation controller in a subclass?

Can anyone tell me about how to implement a navigation controller in a UIView subclass?
I am creating four sub-classes, and I do not want the navigation control in first class, but I need to place it in the second, third and fourth subclass. How can I do that?
Can anybody help me? How do I implement it?
If you dnt want navigation control in first class then you can hide the navigationBar by
self.navController.navigationBarHidden = YES; and this has to be in the next class
self.navController.navigationBarHidden = NO;
When you call 2nd, 3rd and 4th view from 1st one (which does not have navigation bar) then call it as below:
RootViewController * rootViewController = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self.window addSubview:navigationController.view];

How to create Navigation Controller to connect Main View Controller to 2 Custom View Controllers

I need some help on how to create a Navigation Controller that loads its Root View Controller to a Main View Controller. Then, I want to have that Navigation Controller two selector buttons (leftBarButtonItem and rightBarButtonItem) that will take the user to 2 custom View Controllers (AboutViewController and SettingsViewController).
I was able to do this successfully but instead, I created the 2 view controllers inside the MainWindow.xib with all codes inside AppDelegate.h and AppDelegate.m. This is obviously not a good practice especially when you have a lot of custom view controllers.
Thanks.
You are right that it isn't good practice to create your view controllers in the AppDelegate. Instead, you should create a separate ViewController for each view. In the implementation file for your rootViewController, create methods to push the separate view controllers that you want to show when the buttons are pressed. Something like this would work:
- (void)showAboutView
{
AboutViewController *aboutViewController = [[AboutViewController alloc] init];
[self.navigationController pushViewController:aboutViewController animated:YES];
}
- (void)showSettingsView
{
SettingsViewController *settingsViewController = [[SettingsViewController alloc] init];
[self.navigationController pushViewController:settingsViewController animated:YES];
}
Then, include those methods in the selector field for your buttons. Like this:
UIBarButtonItem *aboutBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(showAboutView)];
[[self navigationItem] setRightBarButtonItem:aboutBarButtonItem];
This should take care of pushing your view. The navigationController will automatically show a button to 'go back'.
In the AppDelegate, you would create the navigationController and tell it what view will be the rootViewController, like this:
ViewController1 *vc1 = [[ViewController1 alloc] init];
mainNavigationController = [[UINavigationController alloc] initWithRootViewController:vc1];
Hope this helps!

NavigationController not displayed when used along with TabBarController

In my iphone app, i have a navigation Controller and a tabBar Controller.
The TabBarController has three tabs. In the second and third Tab the NavigationController are added to the viewControllers.
Problem :
In third tab viewController shows the NavigationBar but the in second tab viewController doesnot display navigationBar.
Things I have tried and checked:
1) I checked that all the connections in IB are done properly
2) I checked the size of frame for the view. It doesnot overlap the navigationBar.
3) I also tried using self.navigationController.navigationBar.hidden = NO;
But still it does not show the navigationBar in the second tab.
What should I do?
Please Suggest
Please Help
Thanks!!
We can't do much without looking at your code.
Assuming your TabBarController is properly connected in Interface Builder, you'll need something similar to this:
UIViewController *firstView = [[UIViewController alloc] init];
UIViewController *secondView = [[UIViewController alloc] init];
UIViewController *thirdView = [[UIViewController alloc] init];
UINavigationController *firstNav = [[UINavigationController alloc] initWithRootViewController:secondView];
UINavigationController *secondNav = [[UINavigationController alloc] initWithRootViewController:thirdView];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, firstNav, secondNav, nil];
Of course, have every view released afterwards.
Hmmm thats a problem i faced too.
If you look at your IB file ,you'll see that you can do a bit of tweaking and acheive it.
i'll post details as soon as i get time.
Oops!!! a big mistake on my part.I did not check the checkbox for "show navigation bar" in inspector for NavigationController. Hope this helps someone.

How do I pushViewController/etc. from a UIViewController subclass?

I've been attempting to figure this out for a while now, but I'm up to a point where I can't seem to solve my problem from reading other Q&As. I'm trying to get the active UIViewController in a UINavigationController to send popViewController/pushViewController messages to the UINavigationController, but I cannot figure it out. I'm probably doing something rather stupid that is causing it to break. The structure should be like this, but even then I'm not sure if I've done that right.
mainController
primaryNavigationController
firstViewController
secondViewController
both firstViewController and secondViewController are a subclass
mainController.m
firstViewController = [[FirstTestViewController alloc] init];
secondViewController = [[FirstTestViewController alloc] init];
primaryNavigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
[primaryNavigationController.view setFrame:CGRectMake(0,0,320i,409)];
[self.view addSubview:[primaryNavigationController view]];
[primaryNavigationController.navigationBar setFrame:CGRectMake(0,0,20,44)];
primaryNavigationController.navigationBar.tintColor = [UIColor blackColor];
How can I tell primaryNavigationController to push/pop a VC from within the firstTestViewController subclass?
You would allocate the second view controller within your first view controller (because you don't need it before):
secondViewController = [[FirstTestViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
[secondViewController release];
The SDK includes many sample projects that involve a navigation controller and show you how to do this.

iPhone Dev: Views not fitting in my UINavigation Controller

I don't use IB much for creating my views, so I usually do everything programatically. I'm bascially pushing a viewController to a navigation controller and it seems the top part of the viewController is under the navigation bar. I'm trying to make the view fit correctly.
Here is my code:
navigationController = [[UINavigationController alloc] initWithNibName:nil
bundle:nil];
[window addSubview:navigationController.view];
viewController = [[UIViewController alloc] init];
//this code was just added to try and make it fit. It doesn't change anything
viewController.view.frame = [UIScreen mainScreen].applicationFrame;
[navigationController pushViewController:viewController animated:NO];
I would be careful using the init method with view controllers - instead look at initWithNibName:bundle:.