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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23548088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:06:25  来源:igfitidea点击:

Storyboard navigation controller and tab bar controller

iosuinavigationcontrollerstoryboarduitabbarcontrolleruistoryboard

提问by Matthijn

I am trying to get the following setup in Storyboard.

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

Storyboard

故事板

Where I have a table view in the beginning, and when I tap a cell it needs to transition to the tab bar controller, which works. But now I want a title and an extra navigation bar button in the the 2 most right controllers.

一开始我有一个表格视图,当我点击一个单元格时,它需要转换到标签栏控制器,它可以工作。但现在我想要一个标题和一个额外的导航栏按钮在 2 个最右边的控制器中。

But I can't seem to drag a button to there or when setting the title, nothing shows up. How can I achieve this setup in storyboard?

但是我似乎无法将按钮拖到那里,或者在设置标题时,没有任何显示。如何在故事板中实现此设置?



Updated question, based on answer below.

更新的问题,基于下面的答案。

New setup

新设置

When I have this new setup (thus with an extra navigation controller in between) I can set the title in the storyboard, but when running the app, the added title is not shown.

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

I have uploadeda Xcode project with exactly that setup. Perhaps it can come in handy.

我已经上传了一个具有该设置的 Xcode 项目。也许它可以派上用场。

回答by ErickBergmann

For changing the UINavigationBartitle (with no need to create 2 other UINavigationController) you can just use

要更改UINavigationBar标题(无需创建 2 个其他UINavigationController),您只需使用

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

and for adding the right button use

并添加右键使用

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

on viewDidLoadmethod for each UIViewControllerreferenced from your UITabBarController.

viewDidLoad每种方法UIViewController从您参考UITabBarController

If you want to work with "navigation structures" inside your UIViewControllerfrom TabItemsso you could edit your BUFViewController.mto that:

如果您想在UIViewControllerfrom 中使用“导航结构” TabItems,则可以将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

You have to think as your UITabBarControlleris inside your parent NavigationController, so you want to hide the parent UINavigationBarand show yours. After that, you'll be able to back to your table using popToRootViewControllerAnimated: on the parent's UINavigationController.

你必须认为你UITabBarController在你的父母里面NavigationController,所以你想隐藏父母UINavigationBar并显示你的父母。之后,您将能够使用popToRootViewControllerAnimated: 在父级的UINavigationController.

Hope that helps :)

希望有帮助:)

回答by Cradmon

Just in case anyone was looking for a swift approach:

以防万一有人正在寻找一种快速的方法:

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

The code is best placed in viewDidAppearor viewWillAppearso the title and button change as the different tabs are pressed.

该代码最好放在viewDidAppearviewWillAppear作为不同的选项卡进行按压使标题和按钮改变。

You also wouldn't need the extra navigation controllers with this approach.

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

回答by rdelmar

I looked at your test app, and I do see the title, very faintly, under the navigation bar. You can see both navigation bars if you select the tab bar controller, and uncheck the "Under Top Bars" box. However, this gives you a weird shadow on the navigation bar. I don't know if there's an easy way to fix this, but I don't think this UI with two navigation bars looks good any way. You might want to eliminate the initial navigation controller, and use a modal segue to present the tab bar controller instead. You could add a bar button item to the navigation controllers you still would have to do the dismissal of the modal view controller.

我看了你的测试应用程序,我确实在导航栏下看到了标题,非常模糊。如果您选择标签栏控制器,并取消选中“在顶部栏下”框,您可以看到两个导航栏。但是,这会在导航栏上给您一个奇怪的阴影。我不知道是否有一种简单的方法可以解决这个问题,但我认为这个带有两个导航栏的 UI 无论如何都不好看。您可能希望消除初始导航控制器,而使用模态转场来显示标签栏控制器。您可以向导航控制器添加一个条形按钮项,您仍然需要关闭模态视图控制器。

回答by Anand

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

回答by ColdLogic

You need to replace those view controllers with UINavigationViewControllers.

您需要将这些视图控制器替换为UINavigationViewControllers.

Delete the right two view controllers.

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

Drag drop 2 UINavigationViewControllersonto the storyboard and position them how you want.

将 2 拖放UINavigationViewControllers到情节提要上并按照您的需要放置它们。

Then, you need to hook those new navigation view controllers to the tab bar controller. Ctrl click and drag from the tab bar controller to the navigation controllers. Select "view controllers" from the "Relationship Segue" section of the menu that pops up. Do this for both navigation controllers.

然后,您需要将这些新的导航视图控制器挂钩到选项卡栏控制器。Ctrl 单击并从选项卡栏控制器拖动到导航控制器。从弹出菜单的“Relationship Segue”部分选择“view controllers”。对两个导航控制器执行此操作。