页面视图控制器viewDidLoad()问题

时间:2017-08-15 07:27:59

标签: swift datasource uipageviewcontroller

我正在学习Swift / Xcode并试图创建一个包含三个页面的应用程序,您可以使用页面视图控制器来回滑动。我在UIViewController子类中遇到的问题,更具体地说是viewDidLoad()函数。我收到类似' NameOfMyClass'' NameOfMyClass'没有成员数据源/代理"和"使用未解析的标识符" setViewControllers"。我已经关注了许多教程并检查了其他帖子,但似乎没有人遇到这些问题。当我试图运行这个时,我会得到一个黑屏,现在我因为信号15错误而终止了。

这里是弹出错误的相关代码:

import UIKit

class RootPageViewController: UIViewController, 
UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    lazy var viewControllerList:[UIViewController] = {

        return [self.VCInstance(name: "MissionOne"),
                self.VCInstance(name: "MissionTwo"),
                self.VCInstance(name: "MissionThree")]

    }()

    private func VCInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: 
     nil).instantiateViewController(withIdentifier: name)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self //...has no member 'dataSource' error
        self.delegate = self //...has no member 'delegate' error
        if let MissionOne = viewControllerList.first {
            setViewControllers([MissionOne], direction: .forward, animated: 
         true, completion: nil) 
         //use of unresolved indentifier 'setViewControllers' error
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

我遇到的另一个问题是,页面视图控制器不接受/识别我的类文件以设置为故事板中的自定义类。

提前感谢任何见解或建议。

2 个答案:

答案 0 :(得分:0)

您的RootPageViewController是UIViewController的子类,而不是UIPageViewController

答案 1 :(得分:0)

我看到你在这里混合了两个不同的控制器。

你想要做的是将3个RootPageViewController添加到UIPageViewController,但是在这里你试图将3个RootPageViewControllers添加到RootPageViewController!

修复它的一种方法是使用RootPageViewController和一个单独的UIPageViewController,它将包含所有RootPageViewControllers,如下所示:

// This is a UIPageViewController containing all RootPages
class PagesViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    lazy var viewControllerList:[UIViewController] = {

        return [RootPageViewController.VCInstance(name: "MissionOne"),
            RootPageViewController.VCInstance(name: "MissionTwo"),
            RootPageViewController.VCInstance(name: "MissionThree")]

    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self
        self.delegate = self

        if let MissionOne = viewControllerList.first {
            setViewControllers([MissionOne], direction: .forward, animated: 
     true, completion: nil) 
        }

    }
}

将在上面的UIPageViewController中添加的RootPageViewController:

// This is a UIViewController representing a single page
class RootPageViewController: UIViewController {

    static func VCInstance(name: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: name)
    }

    // Do what you need a page to do...
}

通过这样的构造,您将解决所有错误,并且您还可以在故事板中使用PagesViewController作为自定义类!