简体   繁体   English

iOS-选择选项卡时的“后退”按钮

[英]iOS - Back button on selecting a tab

I have a UITabBarController. 我有一个UITabBarController。 When I select a tab other than the first, I want the tab bar hidden (which I achieved by hidesBottomBarWhenPushed) and the navigation bar to have a back button (not a plain, but default arrowed) (to get to the first tab). 当我选择第一个选项卡之外的其他选项卡时,我希望隐藏选项卡栏(这是通过hidesBottomBarWhenPushed实现的)和导航栏具有后退按钮(不是普通按钮,而是默认的带箭头的按钮)(以进入第一个选项卡)。 How do I achieve this? 我该如何实现?

I'll answer your question below, but I have to say that this is a very non-standard user interface which is mixing metaphors. 我将在下面回答您的问题,但是我不得不说这是一个非常不标准的用户界面,混合了隐喻。 If you really want to push to different view controllers, you really should not be using a tab bar. 如果您确实要推送到其他视图控制器,则实际上应该使用选项卡栏。 You probably should just have simple buttons to go to your secondary view controllers. 您可能应该只具有简单的按钮即可转到辅助视图控制器。 I wouldn't be surprised if Apple would reject an app that used a tab bar like the way you're conceiving of it. 我也不会感到惊讶,如果苹果将拒绝使用像你受孕的方式标签栏的应用程序。

Anyway, to answer your question, you don't want to try to add a "back" button to the secondary views, but rather you just want to properly implement a navigation controller. 无论如何,要回答您的问题,您不想尝试在辅助视图中添加“后退”按钮,而只是想正确实现导航控制器。 Then, when you push to the secondary views, you get the back button automatically. 然后,当您按下辅助视图时,将自动获得后退按钮。 And you also don't have to worry about silliness associated with hiding the tab bar, because when you push to another view, it will be pushed off, too. 而且,您也不必担心与隐藏选项卡栏相关的愚蠢行为,因为当您推送到另一个视图时,它也会被关闭。

So, consider this example, where you have a tab bar with buttons "One", "Two" and "Three". 因此,考虑这个例子,你有按钮“一”标签栏,“二”和“三”。 But, rather than being a proper tab bar controller configuration, you'd have something like this, where "One" is on navigation controller and has a simple tab bar user interface control on it, and if you happen to tap on the second or third tab, it will just push to the respective view. 但是,不是正确的选项卡栏控制器配置,而是类似这样的情况,其中“一个”位于导航控制器上,并具有简单的选项卡栏用户界面控件,如果您碰巧点击第二个或第三个标签,它将仅推送至相应的视图。 Thus, logically, it might look something like this (even if you're not using storyboards, this might help visualize the potential configuration): 因此,从逻辑上讲,它可能看起来像这样(即使您不使用情节提要,这也可能有助于可视化潜在的配置):

示例情节提要

So, on the main view controller ("One"), you'd add a tab bar with an IBOutlet : 因此,主视图控制器(“1”),你会添加有一个标签栏IBOutlet

@property (weak, nonatomic) IBOutlet UITabBar *tabBar;

I'd then configure that tab bar in viewDidLoad , eg: 然后,我将在viewDidLoad配置该选项卡栏,例如:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.tabBar.delegate = self;
    self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}

Clearly, the view controller itself would need to be a UITabBarDelegate , and you'd need a didSelectItem that would perform either a push segue or use your navigationController to pushViewController . 显然,视图控制器本身需要是一个UITabBarDelegate ,并且您需要一个didSelectItem来执行推送选择或将您的navigationController用作pushViewController And, clearly, you will want to change the references to @"Two" and @"Three" with the actual labels you put on your tab bar buttons, but hopefully this gives you the idea: 并且,很明显,您将希望使用在标签栏按钮上放置的实际标签将对@"Two"@"Three"的引用更改为,但是希望这可以为您带来想法:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if ([item.title isEqualToString:@"Two"])
    {
        // put code for pushing to second view controller here
    }
    if ([item.title isEqualToString:@"Three"])
    {
        // put code for pushing to third view controller here
    }
}

I'd also want the main view controller to make sure to set the selected item back to the first item when it reappears after you pop back: 我还希望主视图控制器确保在弹出后重新出现时将所选项目设置回第一项:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.tabBar.selectedItem = [self.tabBar.items objectAtIndex:0];
}

Anyway, if you do it this way, there's no hiding of the tab bar (because it's no longer a controller, but rather just a user interface element on the first tab). 无论如何,如果以这种方式进行操作,则标签栏不会被隐藏(因为它不再是控制器,而只是第一个标签上的用户界面元素)。 This is probably the easiest way to implement your desired user interface. 这可能是实现所需用户界面的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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