不使用静态数据时 UIPageViewController 崩溃

UIPageViewController crashing when not using static data

我有一个可用的页面视图控制器系统(只是要粘贴所有内容,很确定问题出在顶部附近的我的 firebase 观察中):

class IndividualPeopleVC: UIViewController, UIPageViewControllerDataSource  {

var pageViewController: UIPageViewController!


var pageTitles : [String]!
var imageURLS: NSArray!

let currentUser = FIRAuth.auth()?.currentUser?.uid

override func viewDidLoad()

{

    super.viewDidLoad()

    self.pageTitles = []
    self.pageTitles = ["1", "2", "3", "4'"]
  DataService.ds.REF_INTERESTS.child(passedInterest).child("rooms").child(passedRoom).child("users").observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in

        print(snapshot.value)
        if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
            for snap in snapshots {

                let name = snap.key
                let uid = snap.value as! String

                let person = Person(name: name, bio: "", UID: uid)

                if person.UID != self.currentUser {

                 self.pageTitles.append(uid)
                }

        }

    }
self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

    self.pageViewController.dataSource = self


    let startVC = self.viewControllerAtIndex(0) as ContentViewController

    let viewControllers = NSArray(object: startVC)

    self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)

    self.addChildViewController(self.pageViewController)

    self.view.addSubview(self.pageViewController.view)

    self.pageViewController.didMoveToParentViewController(self)

}

func viewControllerAtIndex(index: Int) -> ContentViewController

{

    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {

        return ContentViewController()

    }

    var vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

    vc.titleText = self.pageTitles[index] as! String
    vc.imageFile = self.pageImages[index] as! String

    vc.pageIndex = index

    return vc

}

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?

{


    var vc = viewController as! ContentViewController

    var index = vc.pageIndex as Int

    if (index == 0 || index == NSNotFound)

    {

        return nil

    }


    index--

    return self.viewControllerAtIndex(index)

}

 func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var vc = viewController as! ContentViewController

    var index = vc.pageIndex as Int

    if (index == NSNotFound) {

        return nil

    }

    index++



    if (index == self.pageTitles.count){

        return nil

    }

    return self.viewControllerAtIndex(index)

}



func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int{

    return self.pageTitles.count

}


func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int{

    return 0

}
}

所以基本上大部分只是 copy-pasted 来自另一个来源并且它有效。它从滚动的 pageTitles 数组创建 4 个页面,在另一个 VC 我传递 1、2、3 和 4 并且标签显示数组中的所有内容,滚动视图应该如此。

当我尝试不 hard-code pageTitles 而是从 firebase 中提取它时,问题就来了。

当我注释掉我在 hard-coding pageTitles 数组的位置时,我在发送标题的 ContentViewController 上得到了一个意外的 nil 值,我喜欢 titleLabel.text =(什么已发送过来)。

我想也许用 if/let 来保护它,让 firebase 有时间加载东西会有所帮助,所以我做了

if let title = (what was sent over) {
 titleLabel.text = title
}

发生这种情况时屏幕上没有任何显示,然后如果我向左或向右滚动,我会收到 pagebefore/pageafters 中索引向下的错误。

我感觉这个应该超级简单,很像tableview吧?从 firebase 加载数据,用信息填充数组,使用 if/lets 让 firebase 有时间加载,然后根据数组更新 UI。

我认为您的 DataService 是异步的。数据准备好后,您必须创建 pageviewcontroller。试试这个修改后的 viewDidLoad 方法。

重写 func viewDidLoad()

{

super.viewDidLoad()

self.pageTitles = []

// self.pageTitles = ["1", "2", "3", "4'"] DataService.ds.REF_INTERESTS.child(passedInterest).child("rooms").child(passedRoom).child("users").observeSingleEventOfType(.Value) {(快照:FIRDataSnapshot)在

    print(snapshot.value)
    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for snap in snapshots {

            let name = snap.key
            let uid = snap.value as! String

            let person = Person(name: name, bio: "", UID: uid)

            if person.UID != self.currentUser {

                self.pageTitles.append(uid)
            }

        }

        self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController

        self.pageViewController.dataSource = self


        let startVC = self.viewControllerAtIndex(0) as ContentViewController

        let viewControllers = NSArray(object: startVC)

        self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

        self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)

        self.addChildViewController(self.pageViewController)

        self.view.addSubview(self.pageViewController.view)

        self.pageViewController.didMoveToParentViewController(self)

    }

}