[英]My UIPageViewController keeps crashing
我花了几个小时尝试使用仪器和StackOverFlow进行调试,但没有找到解决方案。 我的UIPageViewController一直崩溃,我真的认为这是因为CPU达到100%,内存可以达到高水平。 但是,我找不到任何阻止以前未显示的View Controller取消分配的变量或任何强引用。 我认为这是PageViewController中的内存问题,但我不确定100%。 我对Swift来说还很陌生,我真的很难尝试首次亮相,因此对您的帮助将不胜感激。 这是我的UIPageViewControllerDataSource
的代码。
import UIKit
class HarishIntroduction: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
weak var initialViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
let viewController = NSArray(object: initialViewController!)
dispatch_async(dispatch_get_main_queue(), {
self.pageViewController.setViewControllers(viewController as? [UIViewController], direction: .Forward, animated: false, completion: nil)
})
self.pageViewController.view.frame = self.view.bounds
self.addChildViewController(pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
return nil
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
else {
return nil
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("MosconeCenter") as? MosconeCenter
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
return nil
}
else {
return nil
}
}
更新:初始视图控制器是“ HarishIntroduction”。
您正在做的事情不是正确的方法,您试图实例化一个UIPageViewController并将初始视图控制器作为视图控制器,而后者又是UIPageViewController的父级。
我在一个单独的类上执行此操作,并且一切正常,尽管在页面控制器层次结构中,我仅创建了两个视图控制器。 您可以添加更多,没有问题。
并且无需在viewDidLoad的main_queue上执行dispatch_async,因为您已经在使用viewDidLoad的main_queue中了,请删除该代码。
import UIKit
class ViewController: UIViewController, UIPageViewControllerDataSource {
var pageViewController: UIPageViewController!
var harishStoryboard:UIStoryboard!
override func viewDidLoad() {
super.viewDidLoad()
self.harishStoryboard = self.storyboard
// Do any additional setup after loading the view.
self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPageViewController") as! UIPageViewController
self.pageViewController.dataSource = self
let initialViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as! HarishIntroduction
let viewController = NSArray(object: initialViewController)
self.pageViewController.setViewControllers(viewController as? [UIViewController], direction: .Forward, animated: false, completion: nil)
self.pageViewController.view.frame = self.view.bounds
self.addChildViewController(pageViewController)
self.view.addSubview(self.pageViewController.view)
self.pageViewController.didMoveToParentViewController(self)
}
func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
return nil
}
if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishIntroduction") as? HarishIntroduction
return correctViewController
}
/*if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}*/
else {
return nil
}
}
func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
if viewController.isKindOfClass(HarishIntroduction) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPlaces") as? HarishPlaces
return correctViewController
}
/*if viewController.isKindOfClass(HarishPlaces) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("WyomingSeminary") as? WyomingSeminary
return correctViewController
}
if viewController.isKindOfClass(WyomingSeminary) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("AppleTree") as? AppleTree
return correctViewController
}
if viewController.isKindOfClass(AppleTree) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("KearneyHighSchool") as? KearneyHighSchool
return correctViewController
}
if viewController.isKindOfClass(KearneyHighSchool) {
let correctViewController = harishStoryboard.instantiateViewControllerWithIdentifier("MosconeCenter") as? MosconeCenter
return correctViewController
}
if viewController.isKindOfClass(MosconeCenter) {
return nil
}*/
else {
return nil
}
}
}
上面的代码对我来说效果很好,如果需要,我可以为您上传项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.