我成功实现了PageViewController,在其中可以在五个页面(ViewController)之间滑动。问题是当我添加一个按钮来选择一个ViewController时。它像预期的那样显示页面,但随后我被卡在该页面上。 PageViewController不再起作用。我无法在页面之间滑动。
import UIKit
class PageViewController: UIPageViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
lazy var subViewControllers:[UIViewController] = {
return [
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
setViewControllers([subViewControllers[0]], direction: .forward, animated: true, completion: nil)
}
// MARK - UIPageViewControllerDataSource
func presentationCount(for pageViewController: UIPageViewController) -> Int {
return subViewControllers.count
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if(currentIndex <= 0) {
return nil
}
return subViewControllers[currentIndex-1]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let currentIndex:Int = subViewControllers.index(of: viewController) ?? 0
if(currentIndex >= subViewControllers.count-1) {
return nil
}
return subViewControllers[currentIndex+1]
}
}
答案 0 :(得分:0)
您可以将PageViewController
的引用传递给子视图控制器,以便可以从子视图控制器调用PageViewController
的方法。例如,创建
var pageViewController : PageViewController?
在ViewController4
中然后在subViewControllers
lazy var subViewControllers:[UIViewController] = {
let viewController4 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController4") as! ViewController4
viewController4.pageViewController = self
return [
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController1") as! ViewController1,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2") as! ViewController2,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController3") as! ViewController3,
viewController4,
UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController5") as! ViewController5,
]
}()
然后可以从PageViewController
调用ViewController4
的任何方法。为简单起见,您可以在scrollToViewController3()
PageViewController
方法
func scrollToViewController3()
{
setViewControllers([subViewControllers[2]], direction: .reverse, animated: true, completion: nil)
}
并在pageViewController?.scrollToViewController3()
中呼叫ViewController4