UIPageViewController变成黑屏

时间:2018-12-22 18:33:08

标签: ios swift uipageviewcontroller pageviews

我有一个欢迎屏幕,我希望人们使用UIPageViewController看到他们启动我的应用程序的第一时间。我认为我正确执行了所有操作,但是一旦我测试启动它,屏幕就会变黑并且什么也不做。有人可以帮我吗?非常感谢!!

import UIKit

class pageViewController: UIPageViewController,     UIPageViewControllerDataSource, UIPageViewControllerDelegate {


override func viewDidLoad() {
    super.viewDidLoad()

    let window = UIWindow()
    let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
    window.rootViewController = pageViewController
    window.makeKeyAndVisible()

    dataSource = self.dataSource
    delegate = self.delegate

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

lazy var orderedViewControllers: [UIViewController] = {
    return [self.newViewController(name: "IntroViewController"),
            self.newViewController(name: "GatherInfoViewController"),
            self.newViewController(name: "DashboardShowViewController"),
            self.newViewController(name: "HomeShowViewController"),
            self.newViewController(name: "FriendShowViewController"),
            self.newViewController(name: "GettingStartedViewController")]
}()



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


func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {

    guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else { return nil }

    let previousIndex = viewControllerIndex - 1

    guard previousIndex >= 0          else { return orderedViewControllers.last }

    guard orderedViewControllers.count > previousIndex else { return nil        }

    return orderedViewControllers[previousIndex]
}

func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
{
    guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else { return nil }

    let nextIndex = viewControllerIndex + 1

    guard nextIndex < orderedViewControllers.count else { return orderedViewControllers.first }

    guard orderedViewControllers.count > nextIndex else { return nil         }

    return orderedViewControllers[nextIndex]
    }
}

1 个答案:

答案 0 :(得分:0)

评论这些行

let window = UIWindow()
let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
window.rootViewController = pageViewController
window.makeKeyAndVisible()
dataSource = self.dataSource
delegate = self.delegate

并替换为

self.dataSource = self
self.delegate = self

也在AppDelegate的{​​{1}}里面,做

didFinishLaunchingWithOptions

window = UIWindow(frame: UIScreen.main.bounds)
window!.rootViewController = PageViewController()
window!.makeKeyAndVisible()

有关完整的编程示例here