UIPageViewController 访问错误错误
UIPageViewController bad access error
我知道这个错误意味着我有一个 NULL 指针,但我不确定为什么会这样,我认为这一定是我在代码中做错了什么。我的索引从 1 开始,因为我希望它从主页的中间视图控制器开始。我正在尝试使页面视图控制器在类似于 snapchat 的视图控制器之间滑动。我有以下代码:
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
}
错误发生在这一行:
self.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) //Error here
错误如下:“线程1:EXC_BAD_ACCESS(code=2, address=0x7fff58d57ff8)
这是我的 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
}
这是我的故事板,其中包含可在其中滑动的视图控制器:
非常感谢任何帮助!
看起来像
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
}
需要:
func viewControllerAtIndex(index : Int) -> UIViewController? {
if((self.viewControllersArray.count == 0) || (index >= self.viewControllersArray.count)) {
return nil
}
return viewControllersArray[index]
}
另外
let selectedIndex = 1
应该是
let selectedIndex = 0
因为 1 将引用第二页。
我知道这个错误意味着我有一个 NULL 指针,但我不确定为什么会这样,我认为这一定是我在代码中做错了什么。我的索引从 1 开始,因为我希望它从主页的中间视图控制器开始。我正在尝试使页面视图控制器在类似于 snapchat 的视图控制器之间滑动。我有以下代码:
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
}
错误发生在这一行:
self.setViewControllers([pageContentViewController!], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil) //Error here
错误如下:“线程1:EXC_BAD_ACCESS(code=2, address=0x7fff58d57ff8)
这是我的 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
}
这是我的故事板,其中包含可在其中滑动的视图控制器:
非常感谢任何帮助!
看起来像
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
}
需要:
func viewControllerAtIndex(index : Int) -> UIViewController? {
if((self.viewControllersArray.count == 0) || (index >= self.viewControllersArray.count)) {
return nil
}
return viewControllersArray[index]
}
另外
let selectedIndex = 1
应该是
let selectedIndex = 0
因为 1 将引用第二页。