user3239274
user3239274

Reputation: 29

iOS - how to set a navigation bar item in view controller?

I'm new to Objective-C and I want to add a UINavigationBar on my CatrgoryBIDViewController. I have proceed UIButton in InstructionBIDViewController.m file that should navigate to CatrgoryBIDViewController. Here is the function code:

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

But it is not setting the UINavigationBar.

Upvotes: 0

Views: 805

Answers (2)

noobzilla
noobzilla

Reputation: 1155

My apologies - After having reread your question, I am going to tweak my answer some.

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

The above should result in your DetailViewController - CategoryBIDViewController being displayed on top of your InstructionBIDViewController and it should have a UINavigationController in it.

Upvotes: 0

Christian
Christian

Reputation: 4641

You should read the documentation here to understand the way a NavigationController is working. For your case:

If your current ViewController (where your proceed-method is implemented) has a NavigationController (is child of it), you can push another ViewController onto the stack of that NavigationController with

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

In this case you do not need to initialize another NavigationController, but in your CatrgoryBIDViewController in viewWillAppear you need to make the NavigationBar visible if it was not before already with

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

If your current ViewController does not have a NavigationController, you can not push another ViewController on top of it and can not show the NavigationBar of it (although you can create your own NavigationBar and add it to the View of the ViewController, but without the usual Navigation-behaviour embedded).

If you open your ViewController programmatically (e. g. from the AppDelegate) you are correct to do so by your call:

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

Upvotes: 1

Related Questions