gpt4 book ai didi

ios - PageViewController 在旋转时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:15 34 4
gpt4 key购买 nike

您好,我有一个 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'

最佳答案

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

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

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

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

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com