如何从属于 UIPageViewController 的 UIViewControllers 之一中更改 UIPageViewController? [英] How do I change the UIPageViewController from WITHIN one of the UIViewControllers that is part of the UIPageViewController?
问题描述
我已经尝试解决这个问题好几个小时了,我一定是误解了 UIPageViewController
的工作原理.
I have been trying to figure this out for hours, and I must be misunderstanding how UIPageViewController
works.
我有 5 个设置屏幕(加载到 UIPageViewController
中的独立视图控制器),我可以向右 + 向左滑动查看.
I have 5 setup screens (separate view controllers that are loaded into the UIPageViewController
) that I can swipe right + left to see.
但是!我希望能够在他们解除对这些视图控制器之一的警报后以编程方式显示下一个视图控制器.
到目前为止,我发现的唯一接近的解决方案是这里的解决方案(https://github.com/jeffaburt/UIPageViewController-Post )允许下一步"按钮,但该按钮不在显示的视图控制器上,而是在根视图控制器上UIPageViewController
.
And so far, the only solution that I've found that gets close, is this solution here ( https://github.com/jeffaburt/UIPageViewController-Post ) that allows a "next" button, but the button isn't on the view controller that's showing, it's on the root view controller that holds the UIPageViewController
.
我将 UIPageViewController
设置为使用此代码显示 5 个不同的视图控制器.
I have the UIPageViewController
set up to show 5 different view controllers, with this code.
private(set) lazy var orderedViewControllers: [UIViewController] = {
return [self.newColoredViewController("setup1"),
self.newColoredViewController("setup2"),
self.newColoredViewController("setup3"),
self.newColoredViewController("setup4"),
self.newColoredViewController("setup5")]
}()
然后 newColoredViewController
函数执行以下操作:
Then the newColoredViewController
function does this:
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)
}
}
我一定缺少一些基本的理解,因为我尝试了很多不同的方法来以编程方式更改页面,但没有任何反应.
I must be missing some fundamental understanding, because I've tried so many different ways to change the page programmatically, and nothing happens.
我试过这样做,但它什么也没做:
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 问题,我通常可以弄清楚,但在这里我被难住了.
Please point me in the right direction!! I can research all day and usually can figure it out thanks to previous stackoverflow questions that are already answered, but here I'm stumped.
提前致谢!!
推荐答案
我想出了一种更简单的方法,可以在我已经建立的结构内完成.在 UIPageViewController 中,添加了这个功能:
I figured out an easier way to do it within the structure I'd already set up. In the UIPageViewController, added this function:
func nextPageWithIndex(index: Int)
{
// let nextWalkthroughVC = newColoredViewController("setup4")
let nextWalkthroughVC = orderedViewControllers[index]
setViewControllers([nextWalkthroughVC], direction: .Forward, animated: true, completion: nil)
}
然后在其中一个设置视图控制器中,我创建了一个运行此代码的按钮操作:
and then in one of the setup View Controllers I created a button action that ran this code:
@IBAction func yeaaa(sender: AnyObject) {
let parent = self.parentViewController as! SetupViewController
parent.nextPageWithIndex(3)
}
(其中SetupViewController"是 UIPageViewController)
(where "SetupViewController" is the UIPageViewController)
YEEHAWWWW 把我逼疯了.
YEEHAWWWW that was driving me nuts.
这是让我想到self.parentViewController as!SetupViewController"和nextPageWithIndex"代码的视频:https://www.youtube.com/watch?v=K3CSBxX5VXA
Here's the video that gave me the idea of "self.parentViewController as! SetupViewController" and the "nextPageWithIndex" code: https://www.youtube.com/watch?v=K3CSBxX5VXA
这篇关于如何从属于 UIPageViewController 的 UIViewControllers 之一中更改 UIPageViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!