简体   繁体   中英

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?

在此处输入图片说明

在此处输入图片说明

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.

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.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM