ios - PageViewController 在旋转时崩溃
<p><p>您好,我有一个 pageViewcontroller,里面有一些 viewcontroller。里面有一个空白 Controller ,里面有一个标签</p>
<p>问题是当我尝试旋转它时它崩溃了</p>
<p>这里是代码</p>
<pre><code>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(, direction: .Forward, animated: true, completion: nil)
pageViewController.didMoveToParentViewController(self)
print(pages)
}
func viewcontrollerAtIndex(index: Int) -> UIViewController {
let vc = storyboard?.instantiateViewControllerWithIdentifier(pages)
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
}
}
</code></pre>
<p>这是控制台日志</p>
<blockquote>
<p>2016-03-13 14:19:16.743 FacebookAlbums <strong>* Assertion failure in -, /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIPageViewController.m:1062
2016-03-13 14:19:16.748 FacebookAlbums *</strong> Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'No view controllers'</p>
</blockquote></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我遇到了同样的问题。这就是我解决它的方法:通过将以下方法添加到 UIPageViewController 实现文件,我再次设置了 UIPageViewController(就像我们第一次那样)以防设备旋转:</p>
<pre><code>-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
</code></pre>
<p>因为大多数时候,UIPageViewController 是模态呈现的,所以在设备旋转时,会再次调用这个 Controller 。</p></p>
<p style="font-size: 20px;">关于ios - PageViewController 在旋转时崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/35969610/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/35969610/
</a>
</p>
页:
[1]