首页 文章

如何将数据从UIViewController传递到UITabBarController?

提问于
浏览
0

在UIViewController中:

class ViewController2: UIViewController {

    var points = 0

    var pressed = false

    @IBOutlet weak var label: UILabel!

    @IBAction func slider(_ sender: UISlider) {
        number = Int(sender.value)
        label.text = String(number)
    }

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

如果按下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

这是我的故事板 . 我用几句话来描述我在图像中想要做的事情 . 如果您需要更清楚的描述,请告诉我 . 谢谢!

3 回答

  • 0

    在你的 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
    }
    
  • 0

    如果 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)
        }
    }
    

    UPDATE

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

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

    UPDATE 2

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

    @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)
        }
    }
    
  • 0

    您可以正常传递数据

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

相关问题