gpt4 book ai didi

iphone - self.navigationController 为空

转载 作者:行者123 更新时间:2023-12-03 18:28:04 43 4
gpt4 key购买 nike

在我的 View Controller 中,我有一个按钮,当按下按钮时,进入导航 Controller ,我的代码如下:

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

最佳答案

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

  1. 通过使用 UINavigationControllerinitWithRootViewController: 方法使 View Controller 成为导航 Controller 的 rootViewController

  2. 通过使用 UINavigationControllerpushViewController: 方法推送 View Controller 。

确保您的 View Controller 已通过上述任何一种方式添加到导航堆栈中。

<小时/>

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

关于iphone - self.navigationController 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7767049/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com