如何在视图控制器上获取导航栏?

时间:2015-06-27 12:02:58

标签: ios objective-c iphone uitableview

我有三个View控制器 A,B,C 。我将导航控制器连接到 A 视图控制器。在 A 我有一些按钮,我已将按钮segue连接到 B 视图控制器。单击按钮,我将转到 B 视图控制器。在 B 视图控制器上我点击表视图项时有 UITableView 我正在启动 C 视图controller.below是该代码

- (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");

    }

}

但是在 C 视图控制器上,导航栏没有出现。我认为C视图控制器没有链接到导航控制器。

5 个答案:

答案 0 :(得分:2)

使用navigationController推送方法在C viewController中显示导航栏

试试这段代码:

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

答案 1 :(得分:1)

使用以下代码

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

您需要使用UINavigationController作为模式来呈现它。

希望它有所帮助。

答案 2 :(得分:1)

使用它:

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

答案 3 :(得分:0)

您可以使用“推送”segue并将ViewController嵌入导航控制器,然后使用其导航控制器的功能,如pushViewControllerpopViewControllerAnimated

答案 4 :(得分:0)

答案都是正确的,但我只是想解释一下......

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

希望这会对你有所帮助,我的解释是可以理解的..干杯