self.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 为空,如何解决这个问题?谢谢。
更新:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当视图控制器位于导航控制器的导航堆栈中时,视图控制器的 navigationController 属性才会返回有效的导航控制器对象。可以通过以下方式将视图控制器添加到导航堆栈中。
通过使用 UINavigationController 的 initWithRootViewController: 方法使视图控制器成为导航控制器的 rootViewController
通过使用 pushViewController: 的方法推送视图控制器UINavigationController。
确保您的视图控制器通过上述任何一种方式添加到导航堆栈中。
编辑:(在 didFinishLaunchingWithOptions: 代码添加到问题之后):
将 didFinishLaunchingWithOptions: 方法更改为此,
Swift 4(版本):
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.
By making the view controller the rootViewController of a navigation controller using initWithRootViewController: method of UINavigationController
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,
Swift 4 (version):
此代码将产生您正在寻找的解决方案:
This code will yield the solution you're looking for:
在 AppDelegate.m 文件中创建第一个视图 RootView 用于导航:
在 myView.m 文件中添加以下代码以从 myView 导航到 myNewView :
In AppDelegate.m file make your first view RootView for Navigation :
In your myView.m file add below code to navigate to myNewView from myView :