2014-03-02 64 views
0

我是Objective-C的新手,我想在我的CatrgoryBIDViewController上添加UINavigationBar。我继续UIButtonInstructionBIDViewController.m文件应导航到CatrgoryBIDViewController。这里是功能代码:iOS - 如何在视图控制器中设置导航栏项目?

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

+0

你在使用故事板吗? – Daniel

+0

no ...我使用的是xib – user3239274

回答

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

以上都会导致您的DetailViewController - CategoryBIDViewController被显示在您的InstructionBIDViewController的顶部,它应该有它UINavigationController

+0

仍然没有在CatrgoryBIDViewController上显示导航栏 – user3239274

+0

UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 显示错误在viewController ....“属性viewController找不到类型instructionBIDViewContoller类型的对象..你的意思是viewcontollercat?” – user3239274

+0

感谢它工作正常.. – user3239274

1

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

如果当前视图控制器(在您进入法实施)具有NavigationController(是它的孩子),你可以把其他的ViewController到该NavigationController的堆栈

[self.navigationController pushViewController:viewControllercat animated:YES]; 

在这种情况下,你不需要初始化另一个NavigationController,但在viewWillAppearCatrgoryBIDViewController你需要做的导航栏可见如果不是之前已经与

[self.navigationController setNavigationBarHidden:NO animated:YES]; 

如果你当前的ViewController没有NavigationController,你不能在它上面推另一个ViewController并且不能显示它的NavigationBar(尽管你可以创建你自己的NavigationBar并将它添加到ViewController的视图中,但是没有通常的导航-behaviour嵌入式)。

如果你打开你的视图控制器编程方式(例如,从AppDelegate中)你是正确的通过你的电话可以这样做:

viewControllercat = [[CatrgoryBIDViewController alloc] initWithNibName:@"CatrgoryBIDViewController" bundle:nil]; 
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:self.viewControllercat]; 
+0

它没有在CatrgoryBIDViewController顶部显示导航栏 – user3239274

相关问题