故事板导航控制器和标签栏控制器

21
我正在尝试在Storyboard中进行以下设置。

Storyboard

在一开始,我有一个表格视图,当我点击单元格时,它需要转换为选项卡栏控制器,这个可以实现。但现在我想在最右边的两个控制器中添加标题和额外的导航栏按钮。
但我似乎无法将按钮拖到那里,或者在设置标题时,没有任何内容显示出来。如何在Storyboard中实现此设置?

根据下面的答案更新问题。

New setup

当我使用这个新的设置(因此在中间添加了一个额外的导航控制器)时,我可以在故事板中设置标题,但是运行应用程序时,添加的标题不会显示。

我已经上传了一个具有完全相同设置的Xcode项目。也许它会有所帮助。


你的问题解决了吗?我遇到了类似的问题。 - Nischal Hada
5个回答

24

如果您想更改 UINavigationBar 的标题(而不需要创建另外两个 UINavigationController),您只需使用以下方法:

[self.parentViewController.navigationItem setTitle:@"Title"];

同时,为了添加右侧按钮,请使用以下代码:

self.parentViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(myRightButton)];

在你的UITabBarController中引用的每个UIViewController上的viewDidLoad方法中。

如果您想在TabItems中使用“导航结构”来处理您的UIViewController,以便进行编辑,则可以将您的BUFViewController.m修改为:

#import "BUFViewController.h"

@interface BUFViewController ()

@end

@implementation BUFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.parentViewController.navigationController setNavigationBarHidden:YES];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done)];
}

-(void)done{
    [self.parentViewController.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
你需要将你的UITabBarController视为在父级NavigationController内部,所以你想要隐藏父级的UINavigationBar并显示你自己的。 之后,你就可以使用父级UINavigationController上的popToRootViewControllerAnimated: 返回到你的表中。
希望这有所帮助 :)

谢谢您的回复。我尝试了您的想法,它确实有效,但也会禁用通常会出现的“返回按钮”(因为它被添加到覆盖导航栏中)。不知道是否应该在这里使用两个导航栏。但是,对于一个(我的第一张截图),我似乎无法添加标题或按钮。 - Matthijn
我看到了你的编辑,确实可以在代码中添加返回按钮,并且它能够正常工作。但是我是否有所遗漏?这个功能是否可以用更加优雅的方式来实现? - Matthijn
1
我已经进行了编辑,应该对你来说很有效 :) - ErickBergmann
如果有人需要的话,这里是Swift中类似的方法:self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)。 - David M

10

以防有人正在寻找快速方法:

tabBarController?.title = "Your Title"
tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button Title", style: UIBarButtonItemStyle.Plain, target: self, action: "rightButtonFunction")

将代码放在 viewDidAppearviewWillAppear 方法中最佳,这样标题和按钮会随不同的选项卡而改变。

使用这种方法,您也不需要额外的导航控制器。


3

我看了你的测试应用程序,确实在导航栏下方很模糊地看到了标题。如果选择选项卡栏控制器, 并取消 "在顶部栏下" 的勾选,您就可以看到两个导航栏。但是,这会在导航栏上产生奇怪的阴影。我不知道是否有简单的方法来解决这个问题,但无论如何,我认为拥有两个导航栏的界面看起来并不好。您可能希望消除最初的导航控制器,并使用模式转换器来呈现选项卡栏控制器。您可以将一个栏按钮项目添加到您仍然需要执行模态视图控制器的导航控制器。


0
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"Overview_Register"])
    {
        WDRegisterViewController *obj=(WDRegisterViewController *)[segue destinationViewController];
        obj.str_Title=@"Edit Profile";
        obj.isRegister=NO;
    }
}

            [self performSegueWithIdentifier:@"Overview_Measure" sender:nil];



    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    WDPeekViewController *Peek = (WDPeekViewController *)[sb instantiateViewControllerWithIdentifier:@"WDPeekViewController"];
 [self.navigationController pushViewController:tabBarController animated:YES];

0

你需要用UINavigationViewControllers替换那些视图控制器。

删除右边的两个视图控制器。

将2个UINavigationViewControllers拖放到故事板上,并按照您想要的位置放置它们。

然后,您需要将这些新的导航视图控制器连接到选项卡栏控制器。从选项卡栏控制器向导航控制器进行Ctrl单击并拖动。从弹出的菜单中选择“关系Segue”部分的“视图控制器”。对于两个导航控制器都要这样做。


是的,我已经尝试过了(我已经更新了我的问题),因为那是我的最初想法。然后我可以在Storyboard Builder中放置一个标题,但是当我运行应用程序时它并没有显示出来。 - Matthijn
你尝试过在故事板的属性检查器视图中明确设置顶部栏,而不是使用“推断”吗? - Kamaros
如果你所说的明确设置是指使用这个输入字段,那么是的。如你所见,我也可以在栏中拖动一个条形按钮。但是当运行该应用程序时,该视图没有显示任何内容,只有空的导航栏。 - Matthijn
1
你可以更改tabBarController的标题,以更新标题并保持导航返回按钮。[[self tabBarController] setTitle:@"First View Controller"];。虽然有点不规范,但它能够正常工作。 - ColdLogic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接