OStack程序员社区-中国程序员成长平台

标题: ios - PageViewController 在旋转时崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:55
标题: ios - PageViewController 在旋转时崩溃

您好,我有一个 pageViewcontroller,里面有一些 viewcontroller。里面有一个空白 Controller ,里面有一个标签

问题是当我尝试旋转它时它崩溃了

这里是代码

class FAPageViewController: UIPageViewController , UIPageViewControllerDataSource, UIPageViewControllerDelegate {

var pages = ["one", "two"]

var pageViewController = UIPageViewController()

override func viewDidLoad() {
    super.viewDidLoad()

    pages.removeAll()
    for (var i = 0; i < 4; i++) {
        var obj = "child"
        pages.append(obj)
    }

    pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
    pageViewController.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + 64, self.view.frame.width, self.view.frame.height - 64)

    addChildViewController(pageViewController)
    self.view.addSubview(pageViewController.view)

    pageViewController.delegate = self
    pageViewController.dataSource = self

    pageViewController.setViewControllers([viewcontrollerAtIndex(0)], direction: .Forward, animated: true, completion: nil)
    pageViewController.didMoveToParentViewController(self)

    print(pages)


}

func viewcontrollerAtIndex(index: Int) -> UIViewController {
    let vc = storyboard?.instantiateViewControllerWithIdentifier(pages[index])
    return vc!
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    if let index = pages.indexOf(viewController.restorationIdentifier!) {

        if index < pages.count - 1 {
            return viewcontrollerAtIndex(index + 1)
        }

    }

    return nil
}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    if let index = pages.indexOf(viewController.restorationIdentifier!) {

        if index > 0 {
            return viewcontrollerAtIndex(index - 1)
        }

    }

    return nil
}

}

这是控制台日志

2016-03-13 14:19:16.743 FacebookAlbums[7550:1624956] * Assertion failure in -[FacebookAlbums.FAPageViewController willAnimateRotationToInterfaceOrientation:duration:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIPageViewController.m:1062 2016-03-13 14:19:16.748 FacebookAlbums[7550:1624956] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No view controllers'



Best Answer-推荐答案


我遇到了同样的问题。这就是我解决它的方法:通过将以下方法添加到 UIPageViewController 实现文件,我再次设置了 UIPageViewController(就像我们第一次那样)以防设备旋转:

-(void)willAnimateRotationToInterfaceOrientationUIInterfaceOrientation)toInterfaceOrientation durationNSTimeInterval)duration

因为大多数时候,UIPageViewController 是模态呈现的,所以在设备旋转时,会再次调用这个 Controller 。

关于ios - PageViewController 在旋转时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35969610/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4