简体   繁体   English

UIPageViewController错误的访问错误

[英]UIPageViewController bad access error

I know the error means I have a NULL pointer, but I am unsure as to WHY that is, and I assume it must be something I did wrong in my code. 我知道错误意味着我有一个NULL指针,但是我不确定为什么,并且我认为这一定是我在代码中做错了。 My index starts at 1 because I want it to start in the middle view controller which is the home page. 我的索引从1开始,因为我希望它从作为主页的中间视图控制器开始。 I am trying to make a page view controller to swipe between view controllers similar to snapchat. 我正在尝试使页面视图控制器在类似于snapchat的视图控制器之间滑动。 I have the following code: 我有以下代码:

import UIKit

class PageViewController: UIPageViewController, UIPageViewControllerDelegate, UIPageViewControllerDataSource {

  var viewControllersArray = [UIViewController]()
  var pageIndex: Int?
  let selectedIndex = 1

  override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.clearColor()

    let vc1 = storyboard?.instantiateViewControllerWithIdentifier("ProfileView") as! ProfileViewController
    let vc2 = storyboard?.instantiateViewControllerWithIdentifier("HomeView") as! HomeViewController
    let vc3 = storyboard?.instantiateViewControllerWithIdentifier("MatchesView") as! MatchViewController

    viewControllersArray.append(vc1)
    viewControllersArray.append(vc2)
    viewControllersArray.append(vc3)

    self.dataSource = self

    let pageContentViewController = self.viewControllerAtIndex(selectedIndex)
    self.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)  //Error here

  }

the error occurs at this line: 该行发生错误:

self.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)  //Error here

The error is as follows: "Thread 1: EXC_BAD_ACCESS(code=2, address=0x7fff58d57ff8) 错误如下:“线程1:EXC_BAD_ACCESS(code = 2,address = 0x7fff58d57ff8)

Here is my viewControllerAtIndex function: 这是我的viewControllerAtIndex函数:

func viewControllerAtIndex(index : Int) -> UIViewController? {
   if((self.viewControllersArray.count == 0) || (index >= self.viewControllersArray.count)) {
     return nil
   }

   let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! PageViewController
   pageContentViewController.pageIndex = index


   return pageContentViewController
}

Here is my storyboard with the view controllers to swipe between: 这是我的情节提要,其中的视图控制器可在以下内容之间滑动:

在此处输入图片说明

Any and all help is greatly appreciated! 任何帮助都将不胜感激!

It looks like 看起来像

func viewControllerAtIndex(index : Int) -> UIViewController? {
   if((self.viewControllersArray.count == 0) || (index >= self.viewControllersArray.count)) {
     return nil
   }

   let pageContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! PageViewController
   pageContentViewController.pageIndex = index


   return pageContentViewController
}

Needs to be: 需要是:

func viewControllerAtIndex(index : Int) -> UIViewController? {
   if((self.viewControllersArray.count == 0) || (index >= self.viewControllersArray.count)) {
     return nil
   }

   return viewControllersArray[index]
}

Additionally 另外

let selectedIndex = 1

should be 应该

let selectedIndex = 0

since 1 will refer to the second page. 因为1将指向第二页。

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

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