如何从第一个选项卡中切换到标签栏控制器的第二个选项卡? [英] How can I segue to the second tab of a tab bar controller from the first tab?

查看:116
本文介绍了如何从第一个选项卡中切换到标签栏控制器的第二个选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际执行segue没有问题,但是当我这样做时,我的标签栏会从视图的底部消失。我已经从TabBarController1到TabBarController2创建了一个故事板segue。

I have no problem actually performing the segue, but when I do, my tab bar disappears from the bottom of the view. I have made a storyboard segue from TabBarController1 to TabBarController2.

我找到了很多关于Objective-C的答案,但没有找到Swift的答案。

I've found a lot of answers for Objective-C, but none for Swift.

这是执行segue的代码:

This is the code for performing the segue:

if requestsArray.count == 0 {
                self.performSegueWithIdentifier("offerSegue", sender: self)
            } else {
                self.performSegueWithIdentifier("confirm1", sender: self)
            }


推荐答案

你不想要塞。 segue创建目标视图控制器的新实例并显示它。

You don't want to segue. A segue creates a new instance of the destination view controller and presents it.

这就是你的标签栏消失的原因。你正在覆盖你的标签栏控制器,它有2个标签,带有你的TabBarController2的新实例。

That's why your tab bar is disappearing. You are covering your tab bar controller, with it's 2 tabs, with a new instance of your TabBarController2.

你想切换到另一个tab。

您要做的是让您拥有的标签栏控制器切换标签。

What you want to do is to ask your owning tab bar controller to switch tabs.

UIViewController 有一个属性tabBarController,可以让你到达你自己的标签栏控制器。

UIViewController has a property tabBarController that lets you get to your owning tab bar controller.

TabBarControllers有一个属性 selectedIndex ,可让你选择一个标签栏控制器的视图控制器成为活动视图控制器。

TabBarControllers have a property selectedIndex that let you select one of a tab bar controller's view controllers to become the active view controller.

所以,向标签栏控制器发送一条消息,要求它切换到另一个标签。

So, send a message to your tab bar controller asking it to switch to the other tab.

OP以外的其他人要求提供说明如何执行此操作的示例代码。我决定创建一个示例项目来说明如何操作。

Other people aside from the OP have asked for sample code illustrating how to do this. I decided to create a sample project illustrating how to do it.

您可以从Github下载它: https://github.com/DuncanMC/TabBarControllers.git

You can download it from Github: https://github.com/DuncanMC/TabBarControllers.git

我创建了一个UIViewController的基类 ATabController 用于由标签栏控制器管理的视图控制器。 ATabController.swift 文件包含一个枚举,用于指示您要选择的标签:

I created a base class of UIViewController ATabController for the view controllers that are managed by the tab bar controller. The ATabController.swift file includes an enum to indicate which tab you want to select:

@objc enum Tab: Int {
  case first = 0
  case second
  case third
}

(请注意,如果您要传递 Tab 到IBActions,因为IBAction方法需要使用Objective-C类型和函数签名。)

(Note that the enum has to be an Objective-C enum if you're going to pass parameters of type Tab to IBActions, since IBAction methods need to use Objective-C types and function signatures.)

它还包括一个协议 TabController

@objc protocol TabController {
  @objc func switchTab(to: Tab)
}

它还定义了一个委托 tabDelegate

It also defines a delegate tabDelegate:

weak var tabDelegate: TabController?

标签栏控制器有一个prepareForSegue( prepare(for:sender :) )它用来使自己作为标签管理的所有视图控制器的 tabDelegate

The tab bar controller has a prepareForSegue (prepare(for:sender:)) that it uses to make itself the tabDelegate of all the view controllers it manages as tabs:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  if let child = segue.destination as? ATabController {
    child.tabDelegate = self
  }

然后它实现了 switchTab(至:)方法:

@objc func switchTab(to: Tab) {
  let index = to.rawValue
  guard let viewControllerCount = viewControllers?.count,
    index >= 0 && index < viewControllerCount  else { return }
  selectedIndex = index
}

在任何一个子视图控制器是标签栏控制器的标签,您可以使用 IBAction 这样的代码来切换标签:

In any of the child view controllers that are tabs of the tab bar controller, you can use IBAction code like this to switch tabs:

@IBAction func handleFirstButton(_ sender: Any) {
  tabDelegate?.switchTab(to: .first)
}

这篇关于如何从第一个选项卡中切换到标签栏控制器的第二个选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆