iosswiftchildviewcontrollerparentviewcontroller

Access ParentViewController from childViewController


I need to access button declared in ParentViewController from childViewController.Below is my code but that's not working.instance of parent view not getting value in it. Please guide what wrong I am doing?

 override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
   
        if (indexPath.row == (productDetailViewModel.count) - 1 ) { //it's your last cell
            //Load more data & reload your collection view
            if let parent = self.navigationController?.parent as? ProductTypeVCont {

                parent.btnRight.isHidden = true
                parent.btnLeft.isHidden = false
               }
            print("scroll at end")
        } else if (indexPath.row == 0 ) {
            print("scroll at begining")
            if let parent = self.navigationController?.parent as? ProductTypeVCont {

                parent.btnRight.isHidden = false
                parent.btnLeft.isHidden = true
               }
          
        } else if (indexPath.row > 1 || indexPath.row == (productDetailViewModel.count) - 1 ) {
            print("scrolling")
            if let parent = self.navigationController?.parent as? ProductTypeVCont {
                
                parent.btnRight.isHidden = false
                parent.btnLeft.isHidden = false
               }
           
        }
}

parent variable is not getting value in any case. I looked on many questions but answers are outdated or not up to mark or incomplete thus posting this question again.


Solution

  • You didn't provide the view hierarchy, so I will answer this based on the most typical way of how parent-child view controllers are used. I can see in your example code that you are invoking self.navigationController?.parent to access the child viewcontroller's parent. However, you only need to invoke self.parent to get the container viewcontroller that manages this child viewcontroller.

    So you would do something like this in your code:

    if let parentVC = self.parent as? ProductTypeVCont {
         parentVC.btnRight.isHidden = false
         ...
    }
    

    Since we don't know what your view hierarchy is, it is not clear what you are referring to as "parent". It is possible that you actually want to access the presenting view controller. In that case, you would use self.presentingViewController instead of self.parent. Here is some additional info on parent view controller and presenting view controller.

    Lastly, it is generally a good idea to keep the logic separate in your child and parent view controllers. In your example code, now your child view controller is dependent on ProductTypeVCont type. You can remove this dependency by using the classic delegate pattern or event-based architecture. There are many examples out there on how to achieve these. It is just a suggestion on how to create loosely coupled apps.