Initializing UINavigationController in AppDelegate with IB rootViewController - objective-c

I'm working in my AppDelegate to make the app's view return to the first view every time it comes from the background.
First off, I have a navigation controller set up in IB as my initial view controller.
Despite this, if I put
if (!self.window.rootViewController.navigationController)
NSLog(#"null rootview navcontroller");
in appDidFinishLaunching the NSLog happens indicating that my window's navigation controller is null.
So, I figured I would try instantiating it myself with:
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.window.rootViewController];
But when I try that I get error:
Pushing a navigation controller is not supported
Which I must confess I don't really understand. I shouldn't be pushing anything?
Thanks!

If the navigation controller is the initial view controller then self.window.rootViewController should be the navigation controller itself.
The type for rootViewController is UIViewController, so you will need to cast as needed.

Related

Detect rootcontroller in iphone sdk

I have using two types of controller in my application i.e. NavigationController and presentViewController.
How can I detect base controller at any instance through code i.e I am using navigation or presentviewcontroller to transist one viewcontroller to another viewcontroller?
Try this for take rootviewcontroller of navigation:
UIViewController *topViewController = [self.navigationController topViewController];
for present modal view controller check out the 'presentingViewController' property of UIViewController and for navigation you can get the array of view controllers NSArray *ArryViewControllers=[self.navigationController viewControllers]; and then get the object at index 0 . this will be the root view controller of that navigation controller.

Objective c : UINavigationViewController unable to load root controller

Hello I have a navigation view controller on the Interface builder and have it placed on a window.rootviewcontroller and then I pushed the initialized view controller to it. I used this line here: [window.rootViewController addSubViews:[navigation view]];
The [navigation view] has no view inside.
But if i create a navigation in code and do the same as before, i can have the navigation appears on the screen.
Does anyone know what is wrong? i have linked the navigation with the Interface Builder as well.
Have you set the class correctly for the "viewcontroller" which is visible inside navigationcontroller(in XIB) within the navigationController hierarchy
You should try this
if you want to set RootViewController
self.window.rootViewController = navigationController;
//navigationController its is rOotViewControler.
if You want to add as SubView.
[self.window addSubview:navigationController.view];
i hope it'll help you.

Does presentModalViewController: add the view controller to the stack?

I have a main navigation controller with a root view controller. In the root view controller, on the push of a button I present second view controller like this:
SecondVC *secondVC = [[SecondVC alloc] initWithNibName:#"SecondVC" bundle:nil];
[self.navigationController presentModalViewController:secondVC animated:YES];
In the second view controller, on the push of an other button, I want to present a third view controller (this time from a Storyboard):
ThirdVC *thirdVC = [[UIStoryboard storyboardWithName:#"Settings" bundle:nil] instantiateInitialViewController];
[self.navigationController presentModalViewController:thirdVC animated:YES];
However this doesn't do anything. I debugged and it turned out, that self.navigationController is nil.
Shouldn't it be the main navigation controller? Or doesn't presentModalViewController: add the view controller to the stack? Do I always have to put a view controller in a navigation controller before presenting id modally?
The new view controller SecondVC is being presented modally, and it's not added to the view controller stack of the navigationController. You need to create a new UINavigationController, and put SecondVC inside the navController before presenting it modally.
You'll need to add something like:
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:secondVC];
[self addChildViewController:navController];
[self.navigationController presentModalViewController:secondVC animated:<#(BOOL)#>]
your view controller while being presented is not inside a navigation controller. And will not have access to the presenting controllers navigation controller.
Furthermore if you push or pop stack items on the navigation controller beneath the modal view controller you will likely not notice anything.
If you want to put the controller in the stack you can alternatively show the view controller yourself.
[self.view addSubView:myViewController.view]
myViewController.view.frame = self.view.bounds;
and to dismiss the view controller you would simply remove it from its superview.
the drawback here is that some of the did and will appear methods are not called on the view controller. Therefore you may want to call them yourself.
But the principal is much the same. And you can easily simulate the presenting animation with the animation system.
Give it a starting point below your form, then start your animation block and put the view.frame to superview.bounds also giving it an animation time. I find that 2 seconds is ok. sometimes less.
at this point the presented view is inside the controller which is on the stack. Now while you cant directly modify the navigation controller within the presented view controller you could set a delegate that tells the original your intentions and therefore the presenting view controller (the one on the navigation stack) can push or pop the view controllers as requested. And the presented view controller will be pushed along with it.
Another positive point is that you can do much like other apps do, and present a semi modal view. With a partially transparent background. this way you can show things happening behind the view even tho they dont directly manipulate it.

loading a UINavigation controller from a UIView

i am designing an app that has a login screen. it is a UIView with username/password and a button to submit. once the user authenticated successfully, i want to load a new xib file that holds a navigation controller and a navigation bar. below the bar i want to load a tableView and switch between other views as i move along with the programming of it.
what i did is create a new class that inherits from UINavigationController and assembled the xib file to include the navigation controller. i hooked it back up to file's owner and i'm loading the navigation controller modally like this:
myNavController* navVC = [[myNavController alloc] initWithNibName:#"navXibFile" bundle:nil];
[self presentModalViewController:navVC animated:YES];
[navVC release];
this works okay as the navigation controller shows up. however, it shows up with no title, even though i've set one up in IB. moreover, the tableView's delegates are hooked up via IB but i cannot even see empty lines. all i see is an empty navigation bar at the top and blank view (one piece) below it.
thank you for your help.
so i figured it out... first it's a design decision right? is the app being managed by a navigation controller? if so (which is my case), expect the main (first) view, that is a login page, all you need to do is to hide the navigation bar in your ViewdidLoad for the main view:
[self.navigationController setNavigationBarHidden:YES animated:YES];
once the user logs in and you push the next view like this:
MainTableViewController* mainTableVC = [[MainTableViewController alloc]
initWithNibName:#"MainTableViewController" bundle:nil];
[self.navigationController pushViewController:mainTableVC animated:YES];
[mainTableVC release];
and lastly, in the ViewDidLoad of the next view controller:
[self.navigationController setNavigationBarHidden:NO animated:YES];
in case your app needs a navigation controller for a specific section of the app but not all if it, you will need to use the VC to manage this, in a similar way the appDelegate manages the sample navigation based sample app.
i hope this helps anyone struggling with wrapping their minds around the design patterns implemented here.
cheers.

Can't push view onto stack

I have a view controller:
#interface DetailViewController : UIViewController
It displays a map and pins are dropped onto this map. When the user tap's the accessory button one of the annotation views I want another view to be pushed in front of the user.
For some or other reason the navigation controller is always null when I run the following code.
hotelDetailViewController = [[HotelDetailViewController alloc] initWithNibName:#"HotelDetailViewController"
bundle:nil];
if (![self navigationController])
{
NSLog(#"navigation controller null");
}
[self.navigationController pushViewController:hotelDetailViewController animated:YES];
What am I doing wrong? At what point to do I need to alloc and init the navigation controller because it seems to be read only?
At what point to do I need to alloc and init the navigation controller because it seems to be read only?
Well, you don't usually set the navigationController property yourself, you would typically have a navigation controller set up from the start and then pass your DetailViewController to the navigation controller, and that's when the property is set.
The section in the View Controllers programming guide about Navigation Controllers explains how you should set up your navigation controller, either with a nib file or programmatically.