我几个小时以来一直试图解决这个问题,我一定是误解了 UIPageViewController
的工作原理。
我有 5 个设置屏幕(加载到 UIPageViewController
中的独立 View Controller ),我可以向右 + 向左滑动来查看。
但是!我希望能够在它们消除其中一个 View Controller 上的警报后以编程方式显示下一个 View Controller 。
到目前为止,我发现的唯一接近的解决方案是这里的解决方案( https://github.com/jeffaburt/UIPageViewController-Post ),它允许“下一步”按钮,但该按钮不在 View Controller 上如图所示,它位于保存 UIPageViewController
的 Root View Controller 上。
我使用此代码将 UIPageViewController
设置为显示 5 个不同的 View Controller 。
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("setup1"),
self.newColoredViewController("setup2"),
self.newColoredViewController("setup3"),
self.newColoredViewController("setup4"),
self.newColoredViewController("setup5")]
}()
然后newColoredViewController
函数执行以下操作:
private func newColoredViewController(color: String) -> UIViewController {
return UIStoryboard(name: "Main", bundle: nil) .
instantiateViewControllerWithIdentifier("\(color)")
}
这是我的viewDidLoad
:
override func viewDidLoad() {
super.viewDidLoad()
dataSource = self
if let SetupViewController = orderedViewControllers.first {
setViewControllers([SetupViewController],
direction: .Forward,
animated: true,
completion: nil)
}
}
我一定缺少一些基本的理解,因为我尝试了很多不同的方法来以编程方式更改页面,但什么也没有发生。
我尝试过这样做,但没有任何作用:
setViewControllers([orderedViewControllers[3]],
direction: .Forward,
animated: true,
completion: nil)
我尝试过这样做,但没有任何作用:
func scrollToNextViewController() {
if let visibleViewController = viewControllers?.first,
let nextViewController = pageViewController(self,
viewControllerAfterViewController: visibleViewController) {
scrollToViewController(nextViewController)
}
}
请给我指明正确的方向!!我可以研究一整天,并且通常可以通过之前已经回答的 stackoverflow 问题找到答案,但在这里我被难住了。
提前致谢!!
最佳答案
我找到了一种更简单的方法来在我已经设置的结构中做到这一点。在UIPageViewController中,添加了这个功能:
func nextPageWithIndex(index: Int)
{
// let nextWalkthroughVC = newColoredViewController("setup4")
let nextWalkthroughVC = orderedViewControllers[index]
setViewControllers([nextWalkthroughVC], direction: .Forward, animated: true, completion: nil)
}
然后在其中一个设置 View Controller 中,我创建了一个运行以下代码的按钮操作:
@IBAction func yeaaa(sender: AnyObject) {
let parent = self.parentViewController as! SetupViewController
parent.nextPageWithIndex(3)
}
(其中“SetupViewController”是 UIPageViewController)
YEEHAWWWW这让我发疯。
这个视频给了我“self.parentViewController as!SetupViewController”和“nextPageWithIndex”代码的想法:https://www.youtube.com/watch?v=K3CSBxX5VXA
关于ios - 如何从属于 UIPageViewController 一部分的 UIViewController 之一内更改 UIPageViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40427327/