ios - 检测 UIPageViewController 的当前页面
<p><p>我想检测当前页面,以便在显示最后一页时修改按钮的标题,并在每次用户从最后一页滑回倒数第二页时重新修改。 </p>
<p>现在这些是我的委托(delegate)方法和我的 ViewControllers 数组:</p>
<pre><code>lazy var VCArr: = {
let firstVC = self.storyboard!.instantiateViewController(withIdentifier: "FirstTutorialPage")
let secondVC = self.storyboard!.instantiateViewController(withIdentifier: "SecondTutorialPage")
let thirdVC = self.storyboard!.instantiateViewController(withIdentifier: "ThirdTutorialPage")
let fourthVC = self.storyboard!.instantiateViewController(withIdentifier: "FourthTutorialPage")
return
}()
</code></pre>
<p>委托(delegate)方法</p>
<pre><code> func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
return nil
}
let previousIndex = viewControllerIndex - 1
guard previousIndex >= 0 else {
return nil
}
guard self.VCArr.count > previousIndex else {
return nil
}
return self.VCArr
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = self.VCArr.index(of: viewController) else {
return nil
}
let nextIndex = viewControllerIndex + 1
guard nextIndex < self.VCArr.count else {
return nil
}
guard self.VCArr.count > nextIndex else {
return nil
}
return self.VCArr
}
</code></pre>
<p>有人可以帮我准确检测显示的最后一页吗?我尝试了一些方法,但每个人都向我展示了错误的索引。这些方法在显示倒数第二页时检测到最后一个索引。</p>
<p>谢谢大家。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我的解决方案在于实现 <code>UIPageViewControllerDelegate</code> 的功能 <code>pageViewController(_:didFinishAnimating:previousViewControllers:transitionCompleted:)</code> 并执行以下操作:</p>
<pre><code>if completed {
if let currentViewController = pageViewController.viewControllers?.first,
let index = pages.index(of: currentViewController) {
currentIndex = index
}
}
</code></pre>
<p>其中 <code>pages</code> 是我的页面 ViewController 的一个属性,我在其中存储要呈现的所有 ViewController ,而 <code>currentIndex</code> 是一个用于跟踪索引的属性当前页面(又名 ViewController ),以便在我的代码的其他部分中使用它。</p></p>
<p style="font-size: 20px;">关于ios - 检测 UIPageViewController 的当前页面,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/45553670/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/45553670/
</a>
</p>
页:
[1]