0

I cannot figure out how to initialize this class. It's a UIViewController, programmatically set as the app's root view controller in AppDelegate, that creates a UIPageViewController which becomes the view of the UIViewController.

The compile error I receive is "Property 'self.rootContentController' not initialized at super.init call.

How do I initialize this?

class MainContainerViewController: UIViewController {


    var container = UIPageViewController()
    var containerStack = [UIViewController]()
    var rootContentController: UIViewController
    var currentContentController: UIViewController
    var containerWidth = UIScreen.main.bounds.width
    var containerHeight = UIScreen.main.bounds.height


    // load view
    override func loadView() {
        super.loadView()

        buildContainerStack()
        buildPageViewController()

    }


    // build container stack
    func buildContainerStack() {
        let contentController0 = FirstViewController()
        let contentController1 = SecondFeaturedViewController()
        let contentController2 = ThirdViewController()
        let contentController3 = FourthViewController()
        containerStack = [contentController0, contentController1, contentController2, contentController3]
        rootContentController = containerStack[0]
    }


    // build page view controller
    func buildPageViewController() {
        container = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        container.dataSource = self
        container.delegate = self
        container.setViewControllers([containerStack[0]], direction: .forward, animated: true, completion: nil)
        container.view.frame = CGRect(x: 0, y: 0, width: containerWidth, height: containerHeight)
        addChildViewController(container)
        view.addSubview(container.view)
        container.didMove(toParentViewController: self)
    }


    // init
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


// data source
extension MainContainerViewController: UIPageViewControllerDataSource {


    // required: returns the view controller before the given view controller
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if viewController == containerStack.first {
            return nil
        }
        return containerStack[containerStack.index(of: viewController)! - 1]
    }


    // required: returns the view controller after the given view controller
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if viewController == containerStack.last {
            return nil
        }
        return containerStack[containerStack.index(of: viewController)! + 1]
    }


}


// delegate
extension MainContainerViewController: UIPageViewControllerDelegate {


    // called after a gesture-driven transition completes
    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if !completed {
            return
        }
        let newIndex = containerStack.index(of: (pageViewController.viewControllers?.first)!)
        currentContentController = containerStack[newIndex!]
        indexRefresh(newIndex!)
    }


}
youareawaitress
  • 387
  • 2
  • 17
  • Please search on an error before posting. And please review the listed of related questions before posting your question. – rmaddy Aug 18 '17 at 18:03
  • The error is telling you that you have two properties, `rootContentController` and `currentContentController` which are neither optional nor have values set during `init` method. If you can't know what values these have when the object is initialized, you have to make those properties optionals (e.g. make them of type `UIViewController?` or `UIViewController!`). – Rob Aug 18 '17 at 18:05
  • @Rob I wish I could shower you with [fill in the blank]. You're beautiful baby. – youareawaitress Aug 18 '17 at 18:18

0 Answers0