Derek
Derek

Reputation: 2112

Use segmented control to switch *and refresh* subviews

I'm using a segmented control to supplement a tab controller and add extra options within that section.

enter image description here

The segmented control switches between three containers, each of which has an embedded tableviewcontainer and i flip between them by showing / hiding as necessary:

@IBOutlet var listPicker: UISegmentedControl! 
@IBOutlet var dueView: UIView!
@IBOutlet var nextView: UIView!
@IBOutlet var doneView: UIView!

switch sender.selectedSegmentIndex{
        case 0: 
        dueView.hidden = false
        nextView.hidden = true
        doneView.hidden = true
    ...
    }

Each view is linked to a UITableViewController in the storyboard:

enter image description here

Rather than just showing (unhiding) the view (which is working), is there a way to refresh its' content too in the same way as the tab bar controller does?

Upvotes: 1

Views: 2848

Answers (1)

AaoIi
AaoIi

Reputation: 8396

First print the following:

print(self.childViewControllers)

Then you will know the Child indices of your ViewController then use the following

    @IBAction func segmentValueChanged(sender: AnyObject) {


        switch (sender.selectedSegmentIndex) {
  case 0:
       dueView.hidden = false
        nextView.hidden = true
        doneView.hidden = true
 // create object from your nextView and refresh it
    var myClass : ViewController = self.childViewControllers[0] as! ViewController
    myClass.tableView.reloadData()
    myClass.viewWillAppear(false)

   break;


  default:
    break;
       }


    }

Lets say you have the following containers and its holding ViewControllers :

dueView.hidden = false
nextView.hidden = true
doneView.hidden = true

If its sorted inside the view of storyboard like that 1- dueView then 2-nextView 3- doneView then the indices should be but again it all depends on how you sorted them to show:

 dueView : self.childViewControllers[0]
 nextView : self.childViewControllers[1]
 doneView : self.childViewControllers[2]

Update:

Since you have Navigation controller before your view controller then you might need to cast to get the topview controller and then reload the data.

Good luck !

Upvotes: 1

Related Questions