Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh Single View Controller in Container View

I need help refreshing a single view controller in a container view by passing a variable value to it. I have a storyboard which contains a view controller with a container view. See the following image:

Container View

Total Sales by Company Users is changing by picker view controller, means it will refresh my view controller for different data.

Now, I'm adding my view controller in this container view of DashbordVC programatically by using following code:

var previousViewController : UIViewController?
var selectedIndex : Int = 0

// TopSalesVC - Instantiate ViewController
private lazy var topSalesVC: TopSalesVC = {
    var vc = TopSalesVC.viewController()
    self.addChildViewController(vc)
    return vc
}()    

// Methods
override func viewDidLoad() {
    super.viewDidLoad()
    self.setupView()
}

//------------------------------------------------------------------------------

func setupView() {

    self.previousViewController = self.topSalesVC
    self.add(asChildViewController: self.topSalesVC)
}

//------------------------------------------------------------------------------

private func add(asChildViewController viewController: UIViewController) {

    self.addChildViewController(viewController)
    self.containerView.addSubview(viewController.view)

    viewController.view.frame = self.containerView.bounds
    viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    viewController.didMove(toParentViewController: self)
}

//------------------------------------------------------------------------------

private func remove(asChildViewController viewController: UIViewController) {
    viewController.willMove(toParentViewController: nil)
    viewController.view.removeFromSuperview()
    viewController.removeFromParentViewController()
}

This code works for when the view loads, but when I change for picker view controller row and click on the Done button, it's does nothing.

Now my code for picker view didSelect row as follows:

override func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    self.selectedIndex = row
}

I have the Done button in the toolbar of picker view. So when I click on it, it executes the following code:

@IBAction func btnDoneClicked(_ sender: UIBarButtonItem) {

    BaseVC.isPickerOpen = false

    self.showPicker(false) { (success) in

        if self.previousViewController != nil {
            self.remove(asChildViewController: self.previousViewController!)
        }

        switch self.selectedIndex {

        case 0:
            .....
            .....
            break

        case 1:
            .....
            .....
            break

        case 2:
            .....
            .....
            break

        case 3:
            .....
            .....
            break

        case 4:
            self.topSalesVC.salesBy = .invoiceByCustomer
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 5:
            self.topSalesVC.salesBy = .invoiceByUser
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 6:
            self.topSalesVC.salesBy = .supplier
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 7:
            self.topSalesVC.salesBy = .manufacturer
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 8:
            self.topSalesVC.salesBy = .masterProduct
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 9:
            self.topSalesVC.salesBy = .user
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 10:
            self.topSalesVC.salesBy = .customer
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        case 11:
            self.topSalesVC.salesBy = .customerTag
            self.previousViewController = self.topSalesVC
            self.add(asChildViewController: self.topSalesVC)
            break

        default:
            break
        }
    }
}

With the above method, from cases 4 to 11 which are using the same view controller, just changes its variable value and adds it to container view.

So my issue is that when I change the picker view value to refresh data for a single view controller, it has no effect on my view controller. Displays the same data and initial state.

I have checked TopSalesVC and there the code is working absolutely fine. So there is no issue with TopSalesVC's Code.

Please help me to fix this issue.

like image 227
Sagar Chauhan Avatar asked Jan 22 '19 05:01

Sagar Chauhan


1 Answers

Try calling the refresh method of your childViewController from viewWillAppear instead of viewDidLoad as viewDidLoad will not be called when adding it as childViewController.

Hope this helps.

like image 90
iPeter Avatar answered Oct 10 '22 04:10

iPeter