Connorelsea
Connorelsea

Reputation: 2468

Change View in parent from child in Swift/XCode 8?

I want to create an on-boarding/registration sequence. For this, I am attempting to use a Scene with a title and a progress bar and a page view. Within that page view will be multiple Scenes for each part of the registration sequence and onboarding. The user will be able to slide through them and it will update the progress bar showing how close they are to the end.

How do I access the progress bar from the child (RegistrationPageViewController) from the view controller of the area with the "Create Account" title?

enter image description here

enter image description here

Upvotes: 0

Views: 640

Answers (2)

anas.p
anas.p

Reputation: 2286

The best way is to implement UIContainerView. It will automatically add as a childviewcontroller to your parent UIViewController. ContainerView can accessible with self.childViewControllers.

for each in self.childViewControllers
{
    if let containerView = each as? <YourContainerView_ViewControllerClass>
    {
         //manage containerView objects here
    }
}

Can also possible to reuse this UIContainerView.

Upvotes: 1

Jake
Jake

Reputation: 13771

A better way to handle this would be to use delegates since the child view controllers do not have access to the parent.

class ParentViewController: UIViewController {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "pageViewController" {
            let dest = segue.destination as? UIPageViewController
            dest?.delegate = self
        }
    }
}

extension ParentViewController: UIPageViewControllerDelegate {
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        //set progress bar by analyzing pageViewController.viewControllers to see which page you're on.
    }
}

Upvotes: 0

Related Questions