如何修复编译错误:在super.init调用时未初始化属性?

时间:2017-08-18 18:02:07

标签: ios swift initialization

我无法弄清楚如何初始化这个类。它是一个UIViewController,以编程方式设置为AppDelegate中应用程序的根视图控制器,它创建一个UIPageViewController,它成为UIViewController的视图。

我收到的编译错误是“属性'self.rootContentController'未在super.init调用时初始化。

如何初始化?

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!)
    }


}

0 个答案:

没有答案