2016-09-23 52 views
0

我有一個工作UIPageViewController保存嵌入UINavigationController多個UIViewControllers每個UIViewController具有圖像陣列的預覽至極,打開時,實例化一個新UIPageViewController來顯示這些圖像BAD_ACCESS上UIPageViewController

時我通過圖像刷卡,然後輕掃回用「EXC_BAD_ACCESS(code=EXC_I386_GPFLT)」同樣的事情,第一個我的應用程序崩潰時我使用的UINavigationController


後退按鈕這是爲什麼,我該如何解決這一問題?

我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() 
    } 
} 

所以結構看起來像
UINavigationController - >DetailPageMasterViewController - >DetailMinionViewController - >PicturesPageMasterViewController - >PicturesMinionViewController

+1

後用線,在應用程序崩潰的代碼這將是有益的。 – pedrouan

+0

也添加一些代碼。 –

+0

現在在if if index == 0 ||之前添加了代碼 – Tobi

回答

-1

請檢查添加到PageController中的viewController的數量。如果可能,你可以分享你的代碼。

+0

檢查它,它似乎是正確的 – Tobi

0

所以我終於想出了什麼導致我的應用程序崩潰。

長話短說:
PicturesMinionViewController一個gestureRecognizer試圖訪問一個已經deinitialized ImageView,我pageViewController工作得很好