【发布时间】:2017-02-10 21:35:40
【问题描述】:
我想用 UIPageViewController 和拖曳按钮(下一个和上一个)来显示 7 个不同内容的视图控制器,这是我实现 UIPageViewController 后的故事板。
我的问题是如何将这些视图控制器放入 UIPageViewController 中?
【问题讨论】:
我想用 UIPageViewController 和拖曳按钮(下一个和上一个)来显示 7 个不同内容的视图控制器,这是我实现 UIPageViewController 后的故事板。
我的问题是如何将这些视图控制器放入 UIPageViewController 中?
【问题讨论】:
用 Swift 回答:
在 UIPageVC 实例的 viewDidLoad 中调用 setViewControllers 方法来定义您的第一个 VC、方向等:
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
let vc = storyboard!.instantiateViewController(withIdentifier: "whatever storyboard id your first VC is")
setViewControllers([vc],
direction: .forward,
animated: false,
completion: nil)
}
使用 UIPageViewControllerDataSource 协议方法来定义当前 viewController 之前和之后的页面以及视图控制器计数和索引。
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerBefore viewController: UIViewController) -> UIViewController?
{
// Return some VC based on what the current viewController param is.
}
func pageViewController(_ pageViewController: UIPageViewController,
viewControllerAfter viewController: UIViewController) -> UIViewController?
{
// Return some VC based on what the current viewController param is.
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return 7
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return viewControllers!.index(of: viewControllers!.first!)!
}
【讨论】: