简体   繁体   中英

My UIPageViewController keeps crashing

I have spent hours trying to debug this using the instruments and StackOverFlow, but I haven't found a solution. My UIPageViewController keeps crashing, and I really think it's because the CPU is reaching 100% and the memory can reach high levels. However, I couldn't find any variables or any strong references that would prevent previous undisplayed View Controllers from deallocating. I think it's a memory issue in the PageViewController, but I am not a 100% sure. I am pretty new to Swift, and I really am having a hard time trying to debut this so any help would be greatly appreciated. Here is the code for my 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
    }
}

UPDATE: The initial view controller is the "HarishIntroduction."

What you are doing is not the right way, you are trying to instantiate a UIPageViewController with an initial view controller as the view controller which is in turn the parent of the UIPageViewController.

I am doing this on a separate class and all works well, I created only two view controller though in the page controller hierarchy. You can add more, no issues with that.

And there is no need for doing a dispatch_async on main_queue on viewDidLoad, because you are already in the main_queue with viewDidLoad, take away that code.

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
    }
}
}

The code above works well for me, if needed I can upload the project for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM