self.navigationController为null

时间:2011-10-14 11:38:13

标签: iphone navigationcontroller

在我的viewcontroller中,我有一个按钮,当按下按钮时,输入navigationController,我的代码如下:

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

但我可以看到self.navigationController为null,如何解决这个问题?谢谢。

更新

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

3 个答案:

答案 0 :(得分:23)

仅当视图控制器位于导航控制器的导航堆栈中时,视图控制器的 navigationController 属性才会返回有效的导航控制器对象。可以通过以下方式将视图控制器添加到导航堆栈。

  1. 使用 UINavigationController

  2. initWithRootViewController:方法,使视图控制器成为导航控制器的rootViewController
  3. 使用 UINavigationController pushViewController:方法推送视图控制器。

  4. 确保以上述任何方式将视图控制器添加到导航堆栈中。


    编辑:(在didFinishLaunchingWithOptions:代码添加到问题之后):

    didFinishLaunchingWithOptions:方法更改为此,

    - (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(版本):

    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
    }
    

答案 1 :(得分:3)

此代码将产生您正在寻找的解决方案:

-(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];    
}

答案 2 :(得分:0)

在AppDelegate.m文件中创建第一个用于导航的RootView视图:

-(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;
}

在myView.m文件中添加以下代码以从myView导航到myNewView:

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

    [obj release];
}