我是 Objective-C 的新手,我想在我的 CatrgoryBIDViewController 上添加一个 UINavigationBar
。我已在 InstructionBIDViewController.m 文件中执行 UIButton
,该文件应导航至 CatrgoryBIDViewController。这是函数代码:
- (IBAction)proceed:(id)sender {
viewControllercat =
[[CatrgoryBIDViewController alloc]
initWithNibName:@"CatrgoryBIDViewController"
bundle:nil];
UINavigationController *nav =
[[UINavigationController alloc]
initWithRootViewController:self.viewControllercat];
//[self.navigationController pushViewController:viewControllercat animated:YES];
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
但它没有设置 UINavigationBar
。
最佳答案
您应该阅读文档 here了解 NavigationController 的工作方式。对于您的情况:
如果您当前的 ViewController(您的 proceed-method 实现的地方)有一个 NavigationController(是它的子级),您可以使用
将另一个 ViewController 插入该 NavigationController 的堆栈[self.navigationController pushViewController:viewControllercat animated:YES];
在这种情况下,您不需要初始化另一个 NavigationController,但是在 viewWillAppear
中的 CatrgoryBIDViewController
中,您需要使 NavigationBar 可见(如果它之前不可见的话)
[self.navigationController setNavigationBarHidden:NO animated:YES];
如果你当前的 ViewController 没有 NavigationController,你不能在它上面推送另一个 ViewController 并且不能显示它的 NavigationBar(虽然你可以创建自己的 NavigationBar 并将它添加到 ViewController 的 View 中,但是没有嵌入通常的导航行为)。
如果您以编程方式打开您的 ViewController(例如,从 AppDelegate),您通过调用这样做是正确的:
viewControllercat = [[CatrgoryBIDViewController alloc] initWithNibName:@"CatrgoryBIDViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewControllercat];
关于iOS - 如何在 View Controller 中设置导航栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22134164/