将数据从视图控制器传递到标签栏控制器

时间:2016-09-20 14:19:27

标签: ios swift uitabbarcontroller pass-data

我尝试使用此代码块将数据从ViewController传递给TabBarController的子项之一:

class LoginViewController: UITabBarController{

//here are the requests and after all are finished the next is
dispatch_group_notify(group, dispatch_get_main_queue()){

    let prefs = NSUserDefaults.standardUserDefaults()
    prefs.setObject("demo", forKey: "USERNAME")
    prefs.setInteger(1, forKey: "ISLOGGEDIN")
    prefs.synchronize()
    print(templates[0])

    let vc = self.tabBarController!.viewControllers![0] as! HomeViewController
    vc.templateForCell = templates

在第二个ViewController中,TabBarController的子代表我有:

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var templateForCell: [VQuizTemplateTo]?

@IBOutlet weak var collection: UICollectionView!

override func viewWillAppear(animated: Bool) {
    collection.delegate = self
    collection.dataSource = self
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? TemplatesCell{

        cell.configureCell(templateForCell!)

        return cell

    } else {
        return UICollectionViewCell()
    }

}


func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

}


func  collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.templateForCell!.count
}


func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    return CGSizeMake(88, 116)
   }   
}

我在error (fatal error: unexpectedly found nil while unwrapping an Optional value)中获得了HomeViewController 我该怎么办?

0 个答案:

没有答案