我在我的应用程序中实现了一个功能性教程介绍,但是当我最后一次滑动屏幕时,我似乎无法将页面视图弹出。我以为dismissViewControllerAnimated:completion:会工作
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
[self.pageViewController dismissViewControllerAnimated:YES completion:nil];
}
return [self viewControllerAtIndex:index];
}
我想出了如何让它消失,但当它到达最后一页时就会消失。我希望它显示最后一页,然后当用户再次在最后一页上滑动时离开。
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
[self.pageViewController.view removeFromSuperview];
}
return [self viewControllerAtIndex:index];
}
答案 0 :(得分:3)
页面视图控制器是你的主要viewcontrollers子视图控制器,这是删除childViewController的正确方法:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((PageContentViewController*) viewController).pageIndex;
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.pageTitles count]) {
[pageViewController willMoveToParentViewController:nil]; // 1
[pageViewController.view removeFromSuperview]; // 2
[pageViewController removeFromParentViewController]; // 3
}
return [self viewControllerAtIndex:index];
}
答案 1 :(得分:0)
同样遇到了同样的问题,我的解决方案是使用以下代码添加一个明确的额外页面:
ValueError: time data '2018-02-16T10:00:49ZParis, Madrid' does not match format '%Y-%m-%dT%H:%M:%SZ%z'
在/// the index of the currently displayed page
fileprivate var currentIndex: Int = 0
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
// didFinishAnimating gets called before viewControllerAfter, which means `currentIndex` will be 1 page behind, hence subtract 2 instead of 1
if currentIndex == myViewControllers.count - 2 {
//self.dismiss(animated: true, completion: nil)
//.... dismiss pages here
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = myViewControllers.index(of: viewController) else { return nil }
let previousIndex = viewControllerIndex - 1
// track current page index
currentIndex = Int(viewControllerIndex)
// ..
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let viewControllerIndex = myViewControllers.index(of: viewController) else { return nil }
let nextIndex = viewControllerIndex + 1
// track page index
currentIndex = Int(viewControllerIndex)
// get next page
if nextIndex < myViewControllers.count {
return myViewControllers[nextIndex]
}
return nil
}
之前调用 didFinishAnimating
。这意味着viewControllerAfter
的值将在调用currentIndex
时用于上一页。因此,为什么我减去2(而不是1)