我将页面视图控制器嵌入到容器视图中。包含“容器”视图的View控制器不是主View控制器,因此我的程序崩溃了。
import UIKit
class CarsSwipe: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
lazy var viewControllersList:[UIViewController] = {
let sb = UIStoryboard(name: "main", bundle: nil).instantiateViewController(withIdentifier: "carsSwipe")
let vc1 = sb.instantiateViewController(withIdentifier: "blueView")
let vc2 = sb.instantiateViewController(withIdentifier: "orangeView")
let vc3 = sb.instantiateViewController(withIdentifier: "blackView")
return [vc1, vc2, vc3]
}()
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllersList.firstIndex(of: viewController) else {return nil}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllersList.count > previousIndex else {return nil}
return viewControllersList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllersList.firstIndex(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllersList.count != nextIndex else {return nil}
guard viewControllersList.count > nextIndex else {return nil}
return viewControllersList[nextIndex]
}
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
if let firsViewControlller = viewControllersList.first {
self.setViewControllers([firsViewControlller], direction: .forward, animated: true, completion: nil)
}
}
}
我希望PageViewController在View控制器内的容器中滚动
答案 0 :(得分:0)
此行中的错误:
let sb = UIStoryboard(name: "main", bundle: nil).instantiateViewController(withIdentifier: "carsSwipe")
删除.instantiateViewController(withIdentifier: "carsSwipe")
,并应成功编译。
下面代码中的sb
不是UIStoryboard
,而是视图控制器:
let vc1 = sb.instantiateViewController(withIdentifier: "blueView")
let vc2 = sb.instantiateViewController(withIdentifier: "orangeView")
let vc3 = sb.instantiateViewController(withIdentifier: "blackView")