I tried to pass the data from ViewController
to one of children of TabBarController
with this block of code:
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
And in the second ViewController, child of TabBarController i have this:
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)
}
}
I got error (fatal error: unexpectedly found nil while unwrapping an Optional value)
in the HomeViewController
.
What would I suppose to do?