Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-10-2009, 09:08 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default Add NavigationController programatically

What is the correct way to add a navigation controller to a view controller (via the code not IB)?

Here is what I've tried. The new sub view is not being added, but the NSLog statement is being printed (in the second group of code):

Code:
- (void)loadView {
	[super loadView];
	//Initialize the navigation controller with the info view controller
	if(navController == nil)
		navController = [[UINavigationController alloc] initWithRootViewController:self];
	
	//Present the navigation controller.
	[self.navigationController presentModalViewController:navController animated:YES];

}
Then later in the code:
Code:
NSLog("Adding info view");
InfoView *infoViewController = [[InfoView alloc] init];
	//[self.view addSubview:infoViewController.view];
	[self.navigationController pushViewController:infoViewController animated:YES];
	[infoViewController release];
ps. Thanks to everyone who helped me today. I am ready to submit my app after this one little bug is removed. Ive been using addSubView instead of pushViewController and I just need to make this one last change.
blueFLoyd8 is offline   Reply With Quote
Old 08-10-2009, 10:08 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default

The view that is calling this is one of the tab bar controller's views. Is this the reason for the problem?

From UINavigationController Class Referene:
- (id)initWithRootViewControllerUIViewController *)rootViewController
Parameters
rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
blueFLoyd8 is offline   Reply With Quote
Old 08-10-2009, 10:21 PM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

You don't add a navigation controller to a view controller; it's the other way around.

Here's an example from my program:

Code:
ManageViewController *manageViewController = [[[ManageViewController alloc] init] autorelease];
UINavigationController *manageNavController = [[[UINavigationController alloc] initWithRootViewController:manageViewController] autorelease];
	
// Couple other controllers set up the same way...
	
[[self tabBarController] setViewControllers: [NSArray arrayWithObjects: tripNavController, prepareNavController, manageNavController, nil]];
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-10-2009, 10:30 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default

I see! I used IB to set up the tab-bar controller, so I guess I cannot do it using code alone. What should I change in IB to make this same change?
blueFLoyd8 is offline   Reply With Quote
Old 08-10-2009, 11:12 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default

In IB I have my Tab bar controller. I changed the first tab's controller class from my own view controller class to UINavigationController.

Then, in the App delegate I added this code. The view never gets shown, only a white view with a top bar (blue)
Code:
firstTabVC = [tabBarController.viewControllers objectAtIndex:0];
MyFirstViewController *FirstVC = [[MyFirstViewController alloc] init];
[firstTabVC.navigationController pushViewController:FirstVC animated:FALSE];
	
   // Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
blueFLoyd8 is offline   Reply With Quote
Old 08-10-2009, 11:19 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

For the IB route, just drag a navigation controller directly into your tab bar. Then find the view controller inside of that and set that to be your custom view controller class.

Then you shouldn't need any code.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-10-2009, 11:35 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 96
Default

Yes! Thanks for the help
blueFLoyd8 is offline   Reply With Quote
Reply

Bookmarks

Tags
navigation controller, pushviewcontroller

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 254
21 members and 233 guests
ADY, bookesp, ckgni, dacapo, Dani77, DarkAn, Davey555, Desert Diva, HemiMG, iDifferent, jakerocheleau, JasonR, LEARN2MAKE, nobre84, prchn4christ, Rudy, ryantcb, Speed, themathminister, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,766
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0