UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:windowObj];
Can i do this?
Making a window as RootViewController with Navigation Controller upon it.
-initWithRootViewController: takes an UIViewController as its argument. Passing an UIWindow instance instead won't work. You can however use the view of the navigation controller as your applications main view.
Related
I've seen a bunch of different tutorials on how this is done. That's fine, I've set up my tab bar controller, have a navigation controller as one of its items, and then set my main view controller as the child of that navigation controller.
If I have an "About" UIBarButtonItem on the main navigation controller's bar, what's the best way of activating the About View Controller? It seems logical that it should be done in the AppDelegate through an IBAction method, but I also kind of feel like it could go in the main view controller some how...
Also, if I'm not initially setting the navigation controller (since I'm setting the tab bar controller as the root), how do I push the about view controller onto its view stack?
Create a UINavigationController and set your AboutViewController as your rootViewController for that navigationView. This way you will be able to set your navigationItem properties and also push view controllers with the self.navigationController property.
You need to set your main view controller under UINavigationController
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController: mainViewController];
Then in your main view controller you customize navigationItem property, you can put your UIBarButtonItem here.
self.navigationItem.rightBarButtonItem = your_about_button_here;
I created a UInavigation controller in my xib file. The UINavigation controller is the top level control. I don't need a parent view. Later on I will manually add the UINavigation controller to the window.
What is the best way to instantiate the UINavigationController? I can't seem to figure out how to do this.
If you do not need to do anything special in the .xib file it is way easier to do this programmatically by adding the following code to your app delegate:
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myView]; //myView is a view you previously created
[self.window addSubview:nav.view];
UIViewController *newView = [[UIViewController alloc] initWithNibName:#"NewView" bundle:[NSBundle mainBundle]];
tabBarController.selectedViewController = newView;
Why doesn't this work?
The selectedViewController property of a UITabBarController must be one of the tab bar's existing tabs (as defined in its viewControllers property). You may want to either push the new controller onto an existing tab (which would have to be a UINavigationController), or add the viewController to the viewControllers array.
Because that view controller isn't part of the UITabBarController.
If you look at the Apple reference for the selectedViewController property, you'd see:
The specified view controller must be
in the viewControllers array.
You need to add the view controller to UITabBarController, using the viewControllers property, so the view controller has a tab, so you can then select that view controller's tab.
In a custom UIViewController, if I have a member UINavigationController that I initialize with self as the root view, like this:
navController = [[UINavigationController alloc] initWithRootViewController:self];
then presenting a modal dialog does not hide the tab bar at the bottom of the screen. The result is that if the user switches to a different tab while a modal dialog is displayed, when they pop back to the tab that was displaying a modal dialog then subsequent calls to presentModalViewController do not display a modal dialog at all, even if I call dismissModalViewControllerAnimated as a result of the tab switch.
If I initialize the UINavigationController with out setting self as the root controller,
navigationController = [[UINavigationController alloc] init];
then the tab bar is hidden as expected. I've changed things in my program so that this isn't really an issue for me anymore, but I'm not sure that I understand why this is happening. Is it considered bad practice to have a navigation controller with self as the root, if the nav controller is going to be displaying modal dialogs?
I never added self as root controller
I always have some Controller that gets a NavigationController.view added to itself.
And the first ViewController that shall be displayed in the Navigation hierarchy I then add as the rootViewController. It's just another word for "first page" (in the beginning all the naming can be quite confusing).
Example in MyProjectAppDelegate.m:
UITableViewController *startScreen = [[UITableViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:startScreen];
[window addSubview:navController.view];
You also shouldn't mix UINavigationController with UITabBarController, if you want to have a tab bar in a navigationcontroller "page" you can build a custom UITabBarController.
This would show you how http://github.com/wiredbob/NavTab
(I had big problems in understanding all this view/controller nesting myself and this project really made the difference. You could say this was the code I really learned how to programm for iPhone/Mac with :DD )
I would like to show a Navigation Controller after clicking a button. Every tutorial assumes the navigation controller will be the first screen so it links it to the App Delegate, but App delegate only appears at MainWindow.xib.
How do you guys add a navigation controller to a view different than the MainWindow?
Thanks!
Here is some sample code to expand on Roger's answer. The following method is linked to some user interaction on the current view controller (to compose an email for example). This will give the compose view the navigation bar across the top instead of coding buttons inside your custom view.
-(void) composeButtonPushed: (id) sender {
ComposeViewController *controller = [[ComposeViewController alloc] initWithNibName:#"ComposeView" bundle:nil];
UINavigationController *composeNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[self presentModalViewController:composeNavController animated:NO];
}
UINavigationController is to navigate a heirarchy of views with UIViewControllers. If you don't have a root UIViewController, it won't work (and doesn;t make sense). If you do have a UIViewController, you simply send a - (id)initWithRootViewController:(UIViewController *)rootViewController init message to a new navigation controller passing in your UIViewController.