简体   繁体   中英

How to get the navigation bar on view controller?

I have three View controller A,B,C .I have navigation controller attached to A view controller.In A i have some buttons,I have attached the button segue to B view controller.Onclick of the button i go to the B view controller.On B View controller i have UITableView on click of table view item i am launching the C view controller.below is the code for that

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row==0)
    {
        NSLog(@"first cell");
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessCard"];
        [self presentViewController:vc animated:YES completion:nil];

    }
    else if(indexPath.row==1)
    {
         NSLog(@"second cell");


    }
    else if(indexPath.row==2)
    {
         NSLog(@"third cell");

    }

}

But on C view controller the navigation Bar is not appearing.I think C view controller is not linked to the Navigation Controller.

You use navigationController push method to display navigation bar in C viewController

try this code:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
        UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessCard"];
        [self.navigationController pushViewController:vc animated:YES];

Use the code below

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessCard"];
UINavigationController *nav=[[UINavigationController alloc] initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:nil];

You need to present it using UINavigationController as modal.

Hope it helps.

用这个:

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

您可以使用“推送” segue并将ViewController嵌入到Navigation Controller中,然后使用其Navigation Controller的功能,例如pushViewControllerpopViewControllerAnimated

Answer are all correct, but i just want to explain things..

//This line gets the storyboard with the name "Main" which contains all
//the setup you made for UI (User interface)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];

//While this like gets the view inside your storyboard with
//storyboard ID/indentifier `BusinessCard `
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"BusinessCard"];

//Lastly, this line is correct presenting the viewcontroller BUT this doesn't add your
//viewcontroller to the array of viewControllers inside navigationController
//
//Also, this line makes you present the viewController above the 
//rootViewController of window which is in your case the navigationController
//
This is you Error
[self presentViewController:vc animated:YES completion:nil];

//This is what you are looking for, and the correct one for your implementation
//
//This will let you add the `vc`(viewController) to the array of viewController 
//in navigationController, to confirm that you can check the `self.navigationController.viewControllers`
//which will return the array of viewController inside your navigationController
This is the Answer
[self.navigationController pushViewController:vc animated:YES];

Hope this helps you, and my explanation is apprehendable.. Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM