我的UIPageViewController不断崩溃

时间:2015-12-28 03:26:28

标签: ios swift automatic-ref-counting uipageviewcontroller crash

我花了好几个小时尝试使用仪器和StackOverFlow进行调试,但我还没有找到解决方案。我的UIPageViewController一直在崩溃,我真的认为它是因为CPU达到了100%并且内存可以达到很高的水平。但是,我无法找到任何变量或任何强大的引用来阻止以前未显示的视图控制器解除分配。我认为它是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。"

1 个答案:

答案 0 :(得分:1)

你正在做的不是正确的方法,你试图用初始视图控制器作为视图控制器实例化一个UIPageViewController,而视图控制器又是UIPageViewController的父视图。

我在一个单独的类上执行此操作并且一切正常,但我在页面控制器层次结构中仅创建了两个视图控制器。你可以添加更多,没有问题。

并且不需要在viewDidLoad上的main_queue上执行dispatch_async,因为您已经使用viewDidLoad在main_queue中,删除该代码。

int a = memcmp(arr1, arr2, sizeof(arr1));
if(!a)
    printf("Arrays are equal\n");
else
    printf("Arrays are not equal\n");

上面的代码对我很有用,如果需要我可以为你上传项目。