lqsdwtxkzlqafpvluq
lqsdwtxkzlqafpvluq

Reputation: 85

How i can get access from Parent ViewController to child PageViewController

I have OnboardingViewController which contains containerView. My containerView - its a OnboardingPageViewController.

How i can get access on my OnboardingViewController to OnboardingPageViewController?

For example from OnboardingViewController i want send action to OnboardingPageViewController. I have my example code, but it doesnt work

enter image description here

My OnboardingViewController

class OnboardingViewController:  BaseViewController, Storyboarded {
    
    @IBOutlet weak var containerView: UIView!
    
    var infoRegisterRefferenceVC : OnboardingPageViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    func saveContrainerViewRefference(vc:OnboardingPageViewController){

        self.infoRegisterRefferenceVC = vc

    }
    
    
    @IBAction func previewAction(_ sender: Any) {
    
    }
    
    
    @IBAction func nextAction(_ sender: Any) {
        // send action to OnboardingPageViewController
        infoRegisterRefferenceVC?.next()
    }
    
}

this is my OnboardingPageViewController

class OnboardingPageViewController: UIPageViewController, UIPageViewControllerDataSource {
    

    lazy var viewControllerList: [UIViewController] = {
        let sb = UIStoryboard(name: "Onboarding", bundle: nil)
        
        let vc1 = sb.instantiateViewController(identifier: "vc1")
        let vc2 = sb.instantiateViewController(identifier: "vc2")
        let vc3 = sb.instantiateViewController(identifier: "vc3")
        let vc4 = sb.instantiateViewController(identifier: "vc4")
        let vc5 = sb.instantiateViewController(identifier: "vc5")
        
        return [vc1, vc2, vc3, vc4, vc5]
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        
         
        let signUpControllerParent = self.parent as? OnboardingViewController
        signUpControllerParent?.saveContrainerViewRefference(vc: self)
        
        if let firstViewController = viewControllerList.first {
            self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
        }
    }
    
    func next() {
// example test code
        self.setViewControllers([viewControllerList[1]], direction: .forward, animated: true, completion: nil)
    }
    
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        return nil
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        return nil
    }
    
    
    
}

My code doesnt work because on this place

let signUpControllerParent = self.parent as? OnboardingViewController
    signUpControllerParent?.saveContrainerViewRefference(vc: self)

signUpControllerParent is nil

Upvotes: 0

Views: 77

Answers (1)

matt
matt

Reputation: 535547

How i can get access on my OnboardingViewController to OnboardingPageViewController?

It is your children.first. You will also need to cast down to OnboardingPageViewController in all likelihood.

As for your actual code, (1) the communication goes the opposite way from what your question asks for, and (2) viewDidLoad is too soon.

Upvotes: 2

Related Questions