1

I am trying to push a new root controller to a navigation stack, but using a side reveal menu.

My app delegate has the following:

    welcomeViewController = [[MyWelcomeViewController  alloc] initWithNibName:@"MyWelcomeViewController" bundle:nil];
navController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController];
navController.navigationBarHidden = YES;

// Then we setup the reveal side view controller with the root view controller as the navigation controller
self.revealSideViewController = [[PPRevealSideViewController alloc] initWithRootViewController:navController];
[self.revealSideViewController setDirectionsToShowBounce:PPRevealSideDirectionNone];
[self.revealSideViewController setPanInteractionsWhenClosed:PPRevealSideInteractionContentView | PPRevealSideInteractionNavigationBar];

// Then we make the window root view controller the reveal side view controller
self.window.rootViewController = self.revealSideViewController;

Once the welcome view controller is displayed, the user logs in. Once logged in the following process runs again from the App Delegate.

self.navController.navigationBarHidden = NO;
[self.navController setTitle:@"Home"];
[self.navController pushViewController:homeViewController animated:NO];

I then have a side view controller setup which is a table view with custom cells setup.

When a row is selected I need to push a new root controller onto the navigation controller. I try this by using the following in the table view for the cell selected.

MyAccountViewController *accountViewController = [[MyAccountViewController alloc] init];
[self.navigationController setViewControllers:[NSArray arrayWithObject:accountViewController] animated:NO];

Unfortunately this does not do anything. If I add the code to the App Delegate and then call the method from the table view controller then it works, however not from the .m file for the table view itself. Adding a log I can see the above is run, just does not do anything.

I am unsure if I need to do anything different on the above. For example, completely pop the views currently shown, then create the navigation controller and PPRevealSideViewController all over again. If I am supposed to, I am unsure how to pop all the current views to then push the new to the window, not from the AppDelegate.

The reason I do not want this in the App Delegate is because it is the incorrect way to approach this, and I would then need a separate method for each new root controller I would like to push from the menu, so the App Delegate would become very large.

enter image description here

StuartM
  • 6,743
  • 18
  • 84
  • 160

2 Answers2

0

Check UINavigationController.h:

@interface UIViewController (UINavigationControllerItem)
@property(nonatomic,readonly,retain) UINavigationController *navigationController; // If this view controller has been pushed onto a navigation controller, return it.

It means when you do myViewController.navigationController you will either get nil if myViewController is not pushed to any navController or the navController reference myViewController is pushed into.

As I understand your tableViewController is not pushed into the navController stack, that means you can't get the navController with tableViewController.navigationController. Instead you'll need to use anyViewControllerInTheStack.navigationController or if the navController is the rootViewController of your keyWindow, by

((UINavigationController*)[[UIApplication sharedApplication] keyWindow].rootViewController)
A-Live
  • 8,904
  • 2
  • 39
  • 74
  • I do not use a UINavigationController.h the navigation controller is setup in code therefore no need for the header and imp files. How could I update the rootViewController with the bottom code provided? – StuartM Nov 05 '12 at 23:09
  • You **do** use and import UINavigationController.h as there's `#import ` somewhere in the app template you started with, usually it is at the prefix header .pch file. – A-Live Nov 06 '12 at 06:56
  • Thanks, I definately feel like this is the exact answer, but I cannot get it to work. Lets say I have a Home View Controller (on the navController stack) then the tableViewController not on the stack. How would I control the navigation controller from the table view? Your last point in your answer mentions controlling from the view controller on the stack, so this case from the Home View Controller. Do you have example code how to accomplish this? Do I need to alloc init my Home View Controller again in the table view to then try and control it? Thanks – StuartM Nov 11 '12 at 20:09
  • @StuartM `self.navigationController` returns you the `navController` reference that has the `self` in the navigation stack. If you want to manage one of the `navControllers` out of it's stack, you'll need to create the reference to one of the controllers in the stack or the `navController` itself somewhere. The beginners are used to create such a reference in the `ApplicationDelegate` as it's easy to be accessed from anywhere (`[UIApplication sharedApplication].delegate`), I'd recommend you to use a separate manager class (probably a singleton) instead to control all the navigation. – A-Live Nov 12 '12 at 13:57
  • Thanks do you have any example code how to use this in a singleton, or any reference how to? I am not sure I understand the last line of code in your answer and how/where this would be used? Thanks for your help! – StuartM Nov 12 '12 at 22:54
0

Add something like this to your AppDelegate.h:

#define XAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])

Now you can access any iVar of AppDelegate from any .m file in your project.

 MyAccountViewController *accountViewController = [[MyAccountViewController alloc] init];
 [XAppDelegate.navController pushViewController:accountViewController animated:NO];

Make sure you add the correct imports. One more thing: It's good to pop the login window from your navcontroller once you are done Logging in.

Hope this helps.

ThefunkyCoder
  • 81
  • 1
  • 7
  • Adding the define line I receive an error missing [, but I am not sure where? – StuartM Nov 05 '12 at 23:08
  • Are you adding it under your imports? I copied it directly from code where I am using this stuff. It works for me. – ThefunkyCoder Nov 07 '12 at 04:55
  • What do you mean under your imports? – StuartM Nov 11 '12 at 20:03
  • In your AppDelegate.h at the top where you call #import `#import ` `#define XAppDelegate ((AppDelegate *)[[UIApplication sharedApplication] delegate])` – ThefunkyCoder Nov 11 '12 at 21:48
  • --Is that where the define should be? – StuartM Nov 11 '12 at 21:49
  • Thanks, I have added the define under the imports in AppDelegate and added the other lines to my .m file, but I receive an error 'Use of undeclared identifier XAppDelegate? The AppDelegate file is imported in the .m file within the project so not sure why this would occur? – StuartM Nov 12 '12 at 22:57
  • Could you paste the code sample here, so that I can be in a better position to know what exactly is happening? You need to post the above code in to AppDelegate.h not .m and have to import the AppeDelegate.h in your viewcontroller class. – ThefunkyCoder Nov 13 '12 at 03:34
  • Thanks, I included the define in the appdelegate.h file then added the second part to my row selection code in the table view controller as it is above, but a slight change to VC names to correct these. I get a few errors which I have added to the question description. – StuartM Nov 14 '12 at 13:04
  • You even would have to synthesize navcontroller in your AppDelegate.m. `@synthesize navcontroller;` – ThefunkyCoder Nov 14 '12 at 17:22
  • Also thats far too less code to determine the exact problem. You might have to paste your imports of both VC and AppDelegate – ThefunkyCoder Nov 14 '12 at 17:23
  • I already synth the navController. Its hard to paste the imports and explain as I would need to import the whole project to best explain. The issue is that the actual left slide view controller is outside of the actual navigation stack so I am unable to update the controller on the nav stack. when I add the define from above into the appdelegate.h file it shows in all one colour is this normal. I understand that the colour scheme might be different not sure if it was something to note. On the left VC I import the AppDelegate.h file. Did you mean I need to synth the XAppDelegate? – StuartM Nov 15 '12 at 17:28