1

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!

1
  • 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. Commented Feb 4, 2018 at 15:53

3 Answers 3

1

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) } } 
Sign up to request clarification or add additional context in comments.

26 Comments

Thank you Milan. But what does 'self.tabBarController?.selectedIndex = 0'?
The ViewController is a child of another TabBarController, so does that work? Thank you!
@ThomasTseng self.tabBarController?.selectedIndex = 0 changes the selected tab in tabBarController.. it is just an example, what you want is to use self.tabBarController property to access UITabBarController and call any property/method on it.. I'll update the answer to provide a better explanation..
@ThomasTseng check it now
@ThomasTseng but this assumes that your ViewController is a child of the CustomTabBarController, if that is not the case, you will have to explain what is your hierarchy..
|
0

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) 

Comments

0

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 } 

1 Comment

Thank you Nitish! But after copying the code, it gave me two errors: 1. Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 2. Cast from 'UITabBarController?' to unrelated type 'SecondViewController' always fails But I thought that the 'SecondViewController' is actually a UITabBarController

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.