self.navigationController 为空

发布于 2024-12-10 00:39:09 字数 825 浏览 0 评论 0原文

在我的视图控制器中,我有一个按钮,当按下按钮时,进入导航控制器,我的代码如下:

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

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

更新:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

带刺的爱情 2024-12-17 00:39:09

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

  1. 通过使用 UINavigationControllerinitWithRootViewController: 方法使视图控制器成为导航控制器的 rootViewController

  2. 通过使用 pushViewController: 的方法推送视图控制器UINavigationController

确保您的视图控制器通过上述任何一种方式添加到导航堆栈中。


编辑:(在 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
}

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
}
金兰素衣 2024-12-17 00:39:09

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

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

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];    
}
掩饰不了的爱 2024-12-17 00:39:09

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文