0

I have a little problem using UINavigationController.

I have a first ViewController (called LaunchViewController) with a navigation controller (that works fine), I can use pushViewController to display a second ViewController (called SettingViewController) and it works fine too, but when (from the first view controller) I want to display a third ViewController (called IPhonePage), after the view is displayed, and when I want to use the navigation controller in my IPhonePage, the navigation controller is null...

Don't know if my question is clear, I can show you the code I use to do that :

AppDelegate.m :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    LaunchViewController* controller = [[LaunchViewController alloc] initWithNibName:@"LaunchViewController" bundle:nil];
    _navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    _navigationController.navigationBar.tintColor = [UIColor blackColor];
    [self.window setRootViewController:_navigationController];
    [self.window makeKeyAndVisible];

    return YES;
}

LaunchViewController.m :

- (IBAction)showSettings:(id)sender {
    SettingViewController* controller = [[SettingViewController alloc] initWithNibName:@"SettingViewController" bundle:nil];
    [self.navigationController pushViewController:controller animated:YES];
}

This works fine (I can access the navigation controller in SettingViewController)

But this,

LaunchViewController.m :

- (void)ShowPageWithIMDevicePage:(id<IMDevicePage>)page {
    [self.navigationController pushViewController:(UIViewController*)page animated:YES];
}

The page is created by :

- (id<IMDevicePage>)CreatePage {
    return [[IPhonePage alloc] init];
}

And my IPhonePage class looks like this,

IPhonePage.h :

@interface IPhonePage : UIViewController <IMDevicePage>

IPhonePage.m :

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"navigation controller = %@", self.navigationController);
}

And the log displays navigation controller = (null), e.g. I don't have access to the navigation controller.

I really hope someone's gonna understand what I want, I know it's a bit complicated but it's for a specific reason that I make that this way.

Thanks ! Cheers :)

EDIT Actually it happens because I create the page, and I set different things like the title, content and toolbar, and then I call pushViewController, the strange thing is that I have a method that sets the content of the UIViewController

IPhonePage.m

- (void)SetContentWithIMDeviceControl:(id<IMDeviceControl>)content {
    self.view = (UIView*)content;
}

And after that it doesn't call anymore my viewDidLoad method (any clue ?). Also I have a method which role is to add a toolbar at the bottom of the view

- (void)SetApplicationBarWithIMDeviceApplicationBar:(id<IMDeviceApplicationBar>)appBar {
}

At first I wanted to use the navigation controller to set this toolbar, but because this method is called before the pushViewController method, the navigation controller is nil. Is there any possibility to set manually the navigation controller without calling pushViewController ? Or any other way to add a toolbar ? Like declaring a toolbar and adding it as a subview maybe ?

4

2 回答 2

1

Try something like :

- (void)ShowPageWithIMDevicePage:(id<IMDevicePage>)page {
    YourDelegate* delegate = (YourDelegate*)[[UIApplication sharedApplication] delegate];
    IPhonePage* pageVC = (IPhonePage*)page;
    [delegate.navigationController pushViewController:pageVC animated:YES];
}
于 2012-11-13T15:27:53.180 回答
0

You can access self.navigationController in - (void)viewWillAppear function. When you create the UIViewController, - (void)viewDidLoad is called. And at this time, you haven't called the pushViewController function and thus self.navigationController is not set.

于 2013-12-08T09:57:26.590 回答