【发布时间】:2017-11-30 11:19:41
【问题描述】:
基本上,我想移动我的 PageViewController 不仅可以通过滑动来移动,还可以通过按钮来移动。
@IBAction func nextButtonAction(_ sender: Any){
}
My PageViewController looks like in this guide
我有什么:
- PageViewController
- 3 个视图控制器
抱歉,如果是重复的,没有找到完全相同的情况
试图在每个 ViewController 上调用 UIPageViewControllerDataSource 但也没有用我认为这不是最好的方法
已编辑: 这是我的 PageViewController
class PageVC: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
lazy var VCarr: [UIViewController] = {
return [self.VCInstance(name: "signUpVerification"),
self.VCInstance(name: "signUpVerification1"),
self.VCInstance(name: "signUpVerification2"),
]
}()
func VCInstance(name:String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:name)
}
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
if let firsVC = VCarr.first {
setViewControllers([firsVC], direction: .forward, animated: true, completion: nil)
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = VCarr.index(of: viewController) else { return nil }
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
return VCarr[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = VCarr.index(of: viewController) else { return nil }
let nextIndex = viewControllerIndex + 1
guard nextIndex < VCarr.count else {
return nil
}
return VCarr[nextIndex]
}
}
extension PageVC {
func goToNextPage(animated: Bool = true, completion: ((Bool) -> Void)? = nil) {
if let currentViewController = VCarr.first {
if let nextPage = dataSource?.pageViewController(self, viewControllerAfter: currentViewController) //for some reasons there's nil {
setViewControllers([nextPage], direction: .forward, animated: animated, completion: completion)
}
}
}
}
这是我称之为 Viewcontroller 的地方:
var pageVC = PageVC()
@IBAction func nextButtonAction(_ sender: Any){
pageVC.goToNextPage()
}
解决方案:
YourViewController: {
@IBAction func nextButtonAction(_ sender: Any){
let pageViewController = self.parent as! PageVC
pageViewController.nextPageWithIndex(index: 1)
}
}
PageViewController: {
func nextPageWithIndex(index: Int) {
setViewControllers([VCarr[index]], direction: .forward, animated: true, completion: nil)
}
}
【问题讨论】:
-
我觉得这个答案是最好的,希望对你有帮助see this link
标签: ios swift uipageviewcontroller