iOS - 如何在视图控制器中设置导航栏项?

时间:2014-03-02 22:01:49

标签: ios iphone objective-c uinavigationbar

我是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

2 个答案:

答案 0 :(得分:1)

您应该阅读文档here以了解NavigationController的工作方式。对于你的情况:

如果您当前的ViewController(实现了proceed-method)有一个NavigationController(它的子代),您可以使用

将另一个ViewController推送到该NavigationController的堆栈上
[self.navigationController pushViewController:viewControllercat animated:YES];

在这种情况下,您不需要初始化另一个NavigationController,但是在CatrgoryBIDViewController viewWillAppear [self.navigationController setNavigationBarHidden:NO animated:YES]; 中,如果已经使用

,则需要使NavigationBar可见
viewControllercat = [[CatrgoryBIDViewController alloc] initWithNibName:@"CatrgoryBIDViewController" bundle:nil];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewControllercat];

如果您当前的ViewController没有NavigationController,则无法在其上推送另一个ViewController,也无法显示它的NavigationBar(尽管您可以创建自己的NavigationBar并将其添加到ViewController的View中,但是没有嵌入通常的导航行为。

如果您以编程方式打开ViewController(例如从AppDelegate打开),那么通过以下方式执行此操作是正确的:

{{1}}

答案 1 :(得分:0)

道歉 - 在重新阅读您的问题后,我将调整一些答案。

-(IBAction)proceed:(id)sender 
{            
    viewControllercat = [[CatrgoryBIDViewController alloc] init];            

    UINavigationController * nc = 
      [[UINavigationController alloc] initWithRootViewController:vc];
    // We now need to display your detail view but cannot push it. 
    // So display modally
    [self presentViewController:nc animated:YES completion:Nil]; 
}

以上结果应该会导致 DetailBIDViewController 上显示 DetailViewController - CategoryBIDViewController ,它应该有一个UINavigationController在它。