简体   繁体   中英

BAD_ACCESS on UIPageViewController

I've got a working UIPageViewController that holds multiple UIViewControllers embedded in an UINavigationController and each UIViewController has a preview of an array of images wich, when opened, instantiate a new UIPageViewController to display those images

when i swipe through the images and then swipe back to the first one my app crashes with " EXC_BAD_ACCESS(code=EXC_I386_GPFLT) " same thing when i use the back button of the UINavigationController


why is that and how can i fix this ?

My PageViewController (the marked line is the last one i got in the debugger before it crashes):

class DetailPageMasterViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource
{
    var presentationPageIndex: Int = 0

    var itemsArray = [Aktion]()
    var pageViewController: UIPageViewController!

    @IBOutlet weak var btnEditOutlet: UIBarButtonItem!
    @IBAction func btnEditAction(sender: AnyObject)
    {


    }
    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.pageViewController = UIPageViewController.init(transitionStyle: .Scroll,
                                                            navigationOrientation: .Horizontal,
                                                            options: nil)

        self.pageViewController.delegate = self
        self.pageViewController.dataSource = self

        self.presentationPageIndex = 0
        let firstVC = self.viewControllerAtIndex(presentationPageIndex)
        let viewControllers = [firstVC]
        self.pageViewController.setViewControllers(viewControllers,
                                direction: .Forward,
                                animated: false,
                                completion: nil)

        self.addChildViewController(self.pageViewController)
        self.view.addSubview(self.pageViewController.view)
        self.pageViewController.didMoveToParentViewController(self)

        self.setupPageControl()
    }

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool)
    {
        if completed
        {
            let minionVC = self.pageViewController.viewControllers?.last as! DetailMinionViewController
            presentationPageIndex = minionVC.pageIndex
        }
    }

    func viewControllerAtIndex(index: Int) -> DetailMinionViewController
    {
        let contentVC = self.storyboard?.instantiateViewControllerWithIdentifier("MinionPageViewController") as! DetailMinionViewController
        contentVC.aktion = itemsArray[index]
        contentVC.pageIndex = index

        return contentVC
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
    {
        if let viewController = viewController as? DetailMinionViewController
        {
            var index = viewController.pageIndex

            if index == 0 || index == NSNotFound
            {
                return nil //MARKED LINE
            }

            index -= 1

            return self.viewControllerAtIndex(index)
        }

        return nil
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController?
    {
        if let viewController = viewController as? DetailMinionViewController
        {
            var index = viewController.pageIndex

            if index == NSNotFound
            {
                return nil
            }

            index += 1

            if index == NSNotFound || index >= itemsArray.count
            {
                return nil
            }

            return self.viewControllerAtIndex(index)
        }

        return nil
    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
    {
        return itemsArray.count
    }

    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
    {
        return presentationPageIndex
    }

    func setupPageControl()
    {
        UIPageControl.appearance().backgroundColor = UIColor.clearColor()
        UIPageControl.appearance().pageIndicatorTintColor = UIColor.whiteColor()
        UIPageControl.appearance().currentPageIndicatorTintColor = UIColor.redColor()
    }
}

So the structure looks like
UINavigationController -> DetailPageMasterViewController -> DetailMinionViewController -> PicturesPageMasterViewController -> PicturesMinionViewController

So i finally figured out what caused my app to crash.

Long story short:
a gestureRecognizer in the PicturesMinionViewController tried to access an already deinitialized ImageView , my pageViewController worked fine

Please check the number of viewController added into the PageController. If possible you can share your code.

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