0

I get data from firebase. Then use (prepareForSegue) to set value for next UIView.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "ToContent" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            firstTB = segue.destinationViewController as! FirstTabBar
            firstTB.clickedRow = indexPath.row
        }
    }
}

FirstTabBar is a TabBarController which contains 3 UIView. How can these 3 UIView classes get that value from FirstTabBar class?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Yunyuen Chan
  • 135
  • 1
  • 11

2 Answers2

2

Simple!

Just pass the data via the segue; assume there are two controllers, firstController and secondController, and the data is theDataToPass which is a class of CoolData

In the controller you are coming from

override func prepareForSegue(segue: NSStoryboardSegue, sender: AnyObject?) {
    let second = segue.destinationController as! SecondController
    second.representedObject = self.theDataToPass
}

and in the controller you are going to

override func viewWillAppear() {
    self.thePassedData = self.representedObject as! CoolData
}
Jay
  • 34,438
  • 18
  • 52
  • 81
0

You can use delegation. I will explain with a bit simpler example with two tab bars but you can transform it to your needs.

First of all, we have to create a protocol and a function that we want to be executed in our desired view controller.

protocol DataDelegate: class {

    func didReceiveData(data: String)
}

Let's say we have FirstViewController which is for the first tabBar and SecondViewController for the second tabBar. In the SecondViewController we have to declare a weak variable for our delegate.

// In SecondViewController
weak var dataDelegate: DataDelegate?

In the FirstViewController we have to conform to the UITabBarControllerDelegate(do not forget to set the delegate to self in viewDidLoad self.tabBarController?.delegate = self) and use the delegate method tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController)

// MARK: - TabBarControllerDelegate

extension FirstViewController: UITabBarControllerDelegate {

    func tabBarController(tabBarController: UITabBarController, didSelectViewController viewController: UIViewController) {
        if let secondViewController = viewController as? SecondViewController {
            secondViewController.dataDelegate = self
        }
    }
}

As we can see viewController is the ViewController that we have tapped on(in our case SecondViewController). We have to cast it to SecondViewController so we have access to it's properties and set dataDelegate to self.

Now we are obligated to conform to the DataDelegate protocol. We have only one function that we have to implement - didReceiveData().

// MARK: - DataDelegateProtocol

extension FirstViewController: DataDelegate {

    func didReceiveData(data: String) {
        print(data)
    }
}

Finally, in our SecondViewController we must tell where we want that function to be executed. I will do it viewDidAppear but you can wherever you need to.

// In SecondViewController
override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    self.dataDelegate?.didReceiveData("data")
}

This guy explains delegation well link. Hope you got it.

gasho
  • 1,926
  • 1
  • 16
  • 20