self.navigationController is null
Asked Answered
N

3

12

in my viewcontroller,I have a button,when press the button,entry the navigationController,my code like:

-(IBAction)ShangHaiButtonPressed:(id)sender{
    marketviewcontroller = [[MarketViewController alloc]initWithNibName:@"MarketViewController" bundle:nil];
    NSLog(@"%@",self.navigationController);
    [self.navigationController pushViewController:marketviewcontroller animated:YES];
    [marketviewcontroller release];   
}

but I can see the self.navigationController is null,how to solve this problem?thank you.

update:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _switchviewcontroller = [[SwitchViewController alloc]initWithNibName:@"SwitchViewController" bundle:nil];
    [self.window addSubview:_switchviewcontroller.view];
    [self.window makeKeyAndVisible];
    return YES;
}
Nought answered 14/10, 2011 at 11:38 Comment(2)
I guess your currentViewController in not in navigation controller stack hierarchy . add currentViewController to navigationcontroller should solve yr problemPricking
In App delegate has the window Object ,In AppDidFinishLaunching method First create object for yr firstcontroller then create NavigationControllerObject like - [[UINavigationController alloc]initWithRootViewController:yrcurrentcontroller]; [self.window addSubView:navigationController.view]Pricking
W
23

The navigationController property of a view controller will return a valid navigation controller object only if the view controller is in a navigation controller's navigation stack. A view controller can be added to a navigation stack in the following ways.

  1. By making the view controller the rootViewController of a navigation controller using initWithRootViewController: method of UINavigationController

  2. By pushing the view controller using pushViewController: method of UINavigationController.

Make sure your view controller is added to the navigation stack in any of the above ways.


EDIT: (After the didFinishLaunchingWithOptions: code added to the question):

Change the didFinishLaunchingWithOptions: method to this,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _switchviewcontroller = [[SwitchViewController alloc]initWithNibName:@"SwitchViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:_switchviewcontroller];
    [self.window addSubview:navController.view];
    [navController release];
    [self.window makeKeyAndVisible];
    return YES;
}

Swift 4 (version):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    switchviewcontroller = SwitchViewController(nibName: "SwitchViewController", bundle: nil)
    let navController = UINavigationController(rootViewController: switchviewcontroller)
    window.addSubview(navController.view)
    window.makeKeyAndVisible()
    return true
}
Wakeen answered 14/10, 2011 at 11:48 Comment(5)
I just have the above code about dealing with navigationController,so what else code I need to write,thank you.Nought
You have window -> currentViewController and now you want to push marketviewcontroller, (ie) window -> currentViewController -> marketviewcontroller right?Wakeen
Alright. Edit your question and paste the didFinishLaunchingWithOptions: code of app delegate there.Wakeen
thank you very much,and I will have a try.if it works ,I will accept your answer,thank you again.Nought
You saved my day. Checked out to find out, that I'm pushing with presentViewController:animatedCarrion
S
3

This code will yield the solution you're looking for:

-(IBAction)ShangHaiButtonPressed:(id)sender {
     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
     [self.view removeFromSuperview];
     [appDelegate.window addSubview:nav.view];   // appDelegate is the delegate of your Application
     marketViewController = [[MarketViewController alloc] initWithNibName:@"MarketViewController" bundle:nil];
     [nav pushViewController:marketViewController animated:YES];
     [marketViewController release];    
}
Sekofski answered 14/10, 2011 at 12:42 Comment(0)
S
0

In AppDelegate.m file make your first view RootView for Navigation :

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    myView *Obj=[[myView alloc]initWithNibName:@"myView" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Obj];
    nav.navigationBar.barStyle = UIBarStyleBlackOpaque;
    [window addSubview:nav.view];
    [self.window makeKeyAndVisible];
    return YES;
}

In your myView.m file add below code to navigate to myNewView from myView :

-(void) registerMethod {
    myNewView *obj = [[myView alloc] initWithNibName:@"myNewView" bundle:nil];
    [self.navigationController pushViewController:obj animated:YES];

    [obj release];
}
Selenography answered 14/10, 2011 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.