在UIViewController前面添加am UIPageViewController

时间:2015-06-04 09:32:15

标签: ios swift uiviewcontroller uipageviewcontroller

当我的应用程序第一次启动时,它应该在第一个应用程序屏幕前显示一个教程,这是一些半透明背景的屏幕。

我尝试创建一个UIPageViewController对象,将其视图添加为当前屏幕的子视图(self.view.addSubview(tutorialViewController.view))并显示屏幕,但是当我滑动时UIPageViewController没有响应该事件。

class TutorialViewController:UIPageViewController, UIPageViewControllerDataSource {
var tutorialsViewControllers = [UIViewController]()


override func viewDidLoad() {
    super.viewDidLoad()
    tutorialsViewControllers.append(Tutorial1ViewController())
    tutorialsViewControllers.append(Tutorial2ViewController())
    tutorialsViewControllers.append(Tutorial3ViewController())
    tutorialsViewControllers.append(Tutorial4ViewController())
    tutorialsViewControllers.append(Tutorial5ViewController())
    self.setViewControllers([tutorialsViewControllers.first!], direction: .Forward, animated: false) { (complete) -> Void in
    }
    self.dataSource = self
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
    if let position = find(self.tutorialsViewControllers, viewController)
        where position > 0
    {
        return self.tutorialsViewControllers[position - 1]
    }
    return nil
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?{
    if let position = find(self.tutorialsViewControllers, viewController)
        where position < self.tutorialsViewControllers.count - 1
    {
        return self.tutorialsViewControllers[position - 1]
    }
    return nil
}
}

视图控制器代码

let tutorialViewController = TutorialViewController()        
self.view.addSubview(tutorialViewController.view)

1 个答案:

答案 0 :(得分:1)

您是否将tutorialViewController添加为子视图控制器?

self.addChildViewController(tutorialViewController)
self.view.addSubview(tutorialViewController.view)