1
votes

In 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
    }
}

I am trying to do something in a TabBarController if a button in a UIViewController is pressed and also add a number to the number in another 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

Here is my storyboard. I've put a few words to describe what I am trying to do in the images. Please tell me if you need a clearer description. Thank you!

3
I am not able to understand your use case, it would be helpful if you could provide us with a clearer description of what you want to achieve.Sam

3 Answers

0
votes

You can pass data as normally

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

In your TabBarController class, take a variable say variableToBeSet

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

Now in your 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
votes

If the ViewController is a child of the UITabBarController that you want to access, you can simply use tabBarController property of the UIViewController, e.g., use this to change selected controller to the first one:

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

    self.tabBarController?.selectedIndex = 0
}   

So let's say that you have a custom UITabBarController subclass, e.g.:

class CustomTabBarController: UITabBarController {


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

Then you can pass it data as follows:

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

UPDATE

Since it seems that the current VC is presented by one of the tabBarController child controllers, you will have to access it through the self.presentingViewController:

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

UPDATE 2

Your screenshot are of a very poor quality, your explanation of the problem would require a clarification too, since it is really hard to understand what you try to do. So after the whole discussion in comments I guess this is it:

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