简体   繁体   English

UIPageViewController上的BAD_ACCESS

[英]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 我有一个工作正常的UIPageViewController ,该UIPageViewController包含嵌入在UINavigationController中的多个UIViewControllers ,并且每个UIViewController都有一个图像阵列的预览,当打开时,实例化一个新的UIPageViewController以显示这些图像

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 当我EXC_BAD_ACCESS(code=EXC_I386_GPFLT)图像然后EXC_BAD_ACCESS(code=EXC_I386_GPFLT)回第一张图像时,当我使用UINavigationController的后退按钮时,我的应用程序因“ EXC_BAD_ACCESS(code=EXC_I386_GPFLT) ”而崩溃


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): 我的PageViewController(标记的行是崩溃之前我进入调试器的最后一行):

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 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 一个gestureRecognizerPicturesMinionViewController试图访问一个已经deinitialized ImageView ,我pageViewController运行良好

Please check the number of viewController added into the PageController. 请检查添加到PageController中的viewController的数量。 If possible you can share your code. 如果可能,您可以共享您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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