iaaqibhussain / ContainerView

Using Container View in iOS with Swift
MIT License
35 stars 7 forks source link

containerview service call (passing data) #3

Closed farazhaider88 closed 5 years ago

farazhaider88 commented 5 years ago

hi hussain,

i m using this in one of my application and im facing issue while calling webservice and passing data. i have a view controller containing buttons and this containerview , containerview has 2 embeded viewcontrollers <ListingVC, MapVc>, one for listing and other for map.

this is the hirarchy -> subcategoriesVC contains buttons of map and listing , with listing as a first viewcontroller of conatainer.

now i need to call webservice to get data for listing and map data, and then i have to pass that data on respective VC of containers, how can i do this ?

1> should i call service on subcategoriesVC or containerview? 2> how can i pass data to both VCs of container?

iaaqibhussain commented 5 years ago

1) you should call the api in subcategoriesVC since its the parent. 2) There is a property of currentViewController inside ContainerViewController. You can use that to pass data. or you can use the viewControllers array that I just committed to pass data to your child controllers.

Hope this helps.

farazhaider88 commented 5 years ago

can you please elaborate this with little example as currentviewController is not accessible in subcategoriesVC?

farazhaider88 commented 5 years ago

Should i do it like this, instead of data object i will send data coming from service ....

class SubCategoriesVC: BaseController {

    var container: ContainerViewController!
    var dataObject =  ["messageObject":"this is new message coming from webservice", "mapObject":"Contains Lat and long"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

      callWebService()
    }

    func callWebService(){

        if container.currentViewController.isKind(of: SubCategoriesListVC.self){

            if let getFirstVCObject = self.container.currentViewController as? SubCategoriesListVC
            {
                getFirstVCObject.dataObject = dataObject
                getFirstVCObject.updateUI()

            }
        }
        else if container.currentViewController.isKind(of: SubCategoriesMapVC.self){

            if let getSecondVCObject = self.container.currentViewController as? SubCategoriesMapVC
            {
                getSecondVCObject.dataObject = dataObject
                getSecondVCObject.updateUI()

            }

        }
    }

    @IBAction func listButtonClicked(){
        container!.segueIdentifierReceivedFromParent("first")

        if container.currentViewController.isKind(of: SubCategoriesListVC.self){

            if let getFirstVCObject = self.container.currentViewController as? SubCategoriesListVC
            {
                getFirstVCObject.dataObject = dataObject
                getFirstVCObject.updateUI()

            }
        }
    }

    @IBAction func mapButtonClicked(){
        container!.segueIdentifierReceivedFromParent("second")

        if let getSecondVCObject = self.container.currentViewController as? SubCategoriesMapVC
        {
            getSecondVCObject.dataObject = dataObject
            getSecondVCObject.updateUI()

        }
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "container"{
            container = segue.destination as! ContainerViewController
            //For adding animation to the transition of containerviews you can use container's object property
            // animationDurationWithOptions and pass in the time duration and transition animation option as a tuple
            // Animations that can be used
            // .transitionFlipFromLeft, .transitionFlipFromRight, .transitionCurlUp

            container.animationDurationWithOptions = (0.2, .transitionCrossDissolve)
        }
    }
iaaqibhussain commented 5 years ago

I think you have not set the identifier for the initial controller through storyboard. That's why currentViewController is inaccessible. in your viewDidLoad try container!.segueIdentifierReceivedFromParent("first")

farazhaider88 commented 5 years ago

yes. thanks . other than that above implementation is correct for passing data ??

iaaqibhussain commented 5 years ago

I'm not sure if its the best practice but it is the correct way to pass data.