我在Xcode的swift中有一个pageViewController。我按照教程制作了一个pageViewController,你可以在页面之间滑动和更改。我不这样做。这是我的pageViewController的代码。
class MainMenue: UIPageViewController {
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newViewController(number: "1"), self.newViewController(number: "2")]
}()
private func newViewController(number:String)-> UIViewController{
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Menu\(number)")
}
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?{
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
return orderedViewControllers[previousIndex]
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = orderedViewControllers.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
let orderedViewControllersCount = orderedViewControllers.count
guard orderedViewControllersCount != nextIndex else {
return nil
}
guard orderedViewControllersCount > nextIndex else{
return nil
}
return orderedViewControllers[nextIndex]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
extension MainMenue: UIPageViewControllerDataSource{
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?{
return nil
}
public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?{
return nil
}
}
我做的唯一不同于教程的是我只使用了2个视图控制器而不是3个。这是我遵循的教程。 https://spin.atomicobject.com/2015/12/23/swift-uipageviewcontroller-tutorial/