【问题标题】:How do I pass data from a UIViewController to UITabBarController?如何将数据从 UIViewController 传递到 UITabBarController?
【发布时间】:2018-07-14 11:30:29
【问题描述】:

在 UIViewController 中:

类 ViewController2: UIViewController { 变量点 = 0 var 按下 = 假 @IBOutlet 弱变量标签:UILabel! @IBAction func滑块(_发件人:UISlider){ number = Int(sender.value) label.text = 字符串(数字) } @IBAction func submitbutton(_ sender: UIButton) { 按下=真 } }

如果 UIViewController 中的按钮被按下,我会尝试在 TabBarController 中执行某些操作,并将数字添加到另一个 TabBarConroller 中的数字。

Image 1: This shows the connection between my ViewControllers.

Image 2: This shows the first two ViewControllers.)

Image 3: This shows the third and fourth ViewController

这是我的故事板。我已经用几句话来描述我在图像中尝试做的事情。如果您需要更清晰的描述,请告诉我。谢谢!

【问题讨论】:

  • 我无法理解您的用例,如果您能向我们提供您想要实现的目标的更清晰描述,将会很有帮助。

标签: swift uiviewcontroller uitabbarcontroller


【解决方案1】:

如果ViewController 是您要访问的UITabBarController 的子对象,您可以简单地使用UIViewControllertabBarController 属性,例如,使用它来将选定的控制器更改为第一个:

@IBAction func submitbutton(_ sender: UIButton) {
    pressed = true

    self.tabBarController?.selectedIndex = 0
}   

假设您有一个自定义的UITabBarController 子类,例如:

class CustomTabBarController: UITabBarController {


    func acceptData(points: Int) {
        print(">>> Accepted: \(points)")
        // or do anything you need to do with it
    }
}

然后你可以按如下方式传递数据:

@IBAction func submitbutton(_ sender: UIButton) {
    pressed = true
    if let customTabController = self.tabBarController as? CustomTabBarController {
        customTabController.acceptData(points: self.points)
    }
}

更新

由于当前 VC 似乎是由 tabBarController 子控制器之一提供的,因此您必须通过 self.presentingViewController 访问它:

@IBAction func submitbutton(_ sender: UIButton) {
    pressed = true
    if let customTabController = self.presentingViewController?.tabBarController as? CustomTabBarController {
        customTabController.acceptData(points: self.points)
    }
}

更新 2

您的屏幕截图质量很差,您对问题的解释也需要澄清,因为很难理解您尝试做什么。所以在 cmets 的整个讨论之后,我想就是这样:

@IBAction func submitbutton(_ sender: UIButton) {
    pressed = true
    if let tabController = self.presentingViewController?.tabBarController,
        let viewController3 = tabController.viewControllers?.filter({ $0 is ViewController3 }).first {

        viewController3.acceptData(points: self.points)
    }
}

【讨论】:

  • 谢谢米兰。但是'self.tabBarController?.selectedIndex = 0'是什么?
  • ViewController 是另一个 TabBarController 的子项,那么这样可以吗?谢谢!
  • @ThomasTseng self.tabBarController?.selectedIndex = 0 更改 tabBarController 中的选定选项卡.. 这只是一个示例,您想要的是使用 self.tabBarController 属性访问 UITabBarController 并在其上调用任何属性/方法..我会更新答案以提供更好的解释..
  • @ThomasTseng 立即查看
  • @ThomasTseng 但这假设您的ViewControllerCustomTabBarController 的子代,如果不是这样,您将不得不解释您的层次结构是什么。..
【解决方案2】:

在你的 TabBarController 类中,取一个变量说 variableToBeSet

class TabBarController: UITabBarController
{
    var variableToBeSet: Int = 0    // This is just an example. You can change it as per requirement.
    // Rest of class implementation
}

现在在你的ViewController

@IBAction func submitbutton(_ sender: UIButton) {
    pressed = true
    let tabControllerInstance = self.tabBarController as! TabBarController
    tabControllerInstance.variableToBeSet = localVariable    // The value which you want to assign
} 

【讨论】:

  • 谢谢尼蒂什!但是在复制代码后,它给了我两个错误:1.线程 1:致命错误:在展开可选值时意外发现 nil 2.从 'UITabBarController?' 转换到不相关的类型'SecondViewController'总是失败但我认为'SecondViewController'实际上是一个UITabBarController
【解决方案3】:

你可以照常传递数据

   let vc:HomeVC = ApiUtillity.sharedInstance.getCurrentLanguageStoryboard().instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
   vc.tempCategoryArray = CategoryArray
   self.navigationController?.pushViewController(vc, animated: true)

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 2018-03-01
    • 2020-10-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    相关资源
    最近更新 更多