【发布时间】:2016-07-18 20:43:05
【问题描述】:
我知道错误意味着我有一个 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
}
这是我的情节提要,其中视图控制器可以在它们之间滑动:
非常感谢任何和所有帮助!
【问题讨论】:
-
Index 函数中的视图控制器在哪里,为什么索引从 1 开始而不是 0?
-
@WMios 我刚刚添加了编辑
-
您能否将堆栈跟踪与实际错误一起包含在内?
标签: ios swift uipageviewcontroller