有 4 个视图:DataViewController、GraphViewController 和 InfoViewController。PageViewController 用于在其他 3 个之间切换。
我已经描述了通过手势滑动我的视图的所有内容。有用。但是,如何通过从 3 个视图中的任何一个单击“页面控制”对象上的点来在另一个视图中使用它?
import UIKit
class PageViewController: UIPageViewController
{
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.ListedViewController(name: "Data"),
self.ListedViewController(name: "Graph"),
self.ListedViewController(name: "Info")]
}()
override func viewDidLoad()
{
super.viewDidLoad()
dataSource = self
if let firstViewController = orderedViewControllers.first {
setViewControllers([firstViewController],
direction: .forward,
animated: true,
completion: nil)
}
}
private func ListedViewController(name: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewController(withIdentifier: "\(name)ViewController")
}
}
// MARK: UIPageViewControllerDataSource
extension PageViewController: UIPageViewControllerDataSource
{
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController?
{
guard let viewControllerIndex = orderedViewControllers.index(of: viewController)
else
{
return nil
}
let previousIndex = viewControllerIndex - 1
// User is on the first view controller and swiped left to loop to
// the last view controller.
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
let orderedViewControllersCount = orderedViewControllers.count
// User is on the last view controller and swiped right to loop to
// the first view controller.
guard orderedViewControllersCount != nextIndex else
{
return orderedViewControllers.first
}
guard orderedViewControllersCount > nextIndex else
{
return nil
}
return orderedViewControllers[nextIndex]
}
func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
return orderedViewControllers.count
}
func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
guard let firstViewController = viewControllers?.first,
let firstViewControllerIndex = orderedViewControllers.index(of: firstViewController) else {
return 0
}
return firstViewControllerIndex
}
}
我应该实现对象吗
页面控制
通过:sender: PageViewController
?