简体   繁体   English

将数据从标签栏 controller 传输到 ViewController

[英]Transfer data from tab bar controller to ViewController

I have a typical ViewController:我有一个典型的 ViewController:

class ViewController: UIViewController{
override func viewDidLoad() {}
}

And I also have TabBarController which displays me the number of the current scene:而且我还有 TabBarController 显示当前场景的编号:

class TabBarController: UITabBarController {
  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
    if item == (self.tabBar.items as! [UITabBarItem])[0]{
      print(1)
    }
    else if item == (self.tabBar.items as! [UITabBarItem])[1]{
      print(2)
    }
  }

}

I need my ViewController to know the number of its scene.我需要我的 ViewController 知道它的场景数量。 (1 or 2). (1 或 2)。 How can I deliver him this number for use from TabBarController?我怎样才能从 TabBarController 向他提供这个号码以供使用?

What you want to do is, add a property called number , or whatever you like, to your ViewController class.您想要做的是,将一个名为number的属性或任何您喜欢的属性添加到您的ViewController class。

class ViewController: UIViewController {
  var number: Int!
}

Then in tabBarController's viewDidLoad , loop through the view controllers, check if they are of type ViewController , and then set their number property to the index of the tabBarController's view controllers:然后在 tabBarController 的viewDidLoad中,遍历视图控制器,检查它们是否属于ViewController类型,然后将它们的number属性设置为 tabBarController 的视图控制器的索引:

class TabBarController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
    for (index,vc) in (viewControllers!.enumerated())! {
      if let viewController = vc as? ViewController {
        vc.number = index
      }
    }
  }

  override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
    if item == (self.tabBar.items as! [UITabBarItem])[0]{
      print(1)
    }
    else if item == (self.tabBar.items as! [UITabBarItem])[1]{
      print(2)
    }
  }

}

Unless, are you wanting to set the value whenever the tab is selected?除非,您想在选择选项卡时设置值吗?

暂无
暂无

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

相关问题 在一个标签栏控制器中从一个ViewController切换到另一个ViewController? - Segue from one ViewController to another ViewController inside a Tab Bar Controller? 是否可以将标签栏控制器类中从 Firebase 检索到的数据从标签栏控制器传输到视图控制器? Xcode,斯威夫特 - Is it possible to transfer data retrieved from Firebase in the tab bar controller class from the tab bar controller to a view controller? Xcode, Swift 如何在标签栏iOS中将数据从ViewController传递到ViewController? - How to pass data from ViewController to a ViewController in a tab bar iOS? Swift 4-将数据从ViewController传递到ViewController到标签栏 - Swift 4 - pass data from ViewController to a ViewController to a tab bar ios:如何使用StoryBoards从ViewController打开Tab Bar控制器 - ios: How open Tab Bar controller from ViewController using StoryBoards 从 viewController 移动到标签栏时的 UI 失真 controller - UI Distortion on moving from viewController to tab bar controller 通过未链接的ViewController的标签栏实例化View Controller - Instantiate View controller with its tab bar from unlinked viewcontroller 如何从嵌入在标签栏 Controller 中的 UIButton 推送 ViewController - How to push a ViewController from a UIButton that is embedded in Tab Bar Controller 如何将数据从导航栏上的右键传输到另一个 ViewController? - How to transfer the data from a right button on Navigation Bar to another ViewController? 标签栏控制器中子标签之间的数据传输 - Data transfer between child tabs in a Tab bar controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM