0

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?

anna.O
  • 101
  • 1
  • 1
  • 7
  • numberOfRowsForSection implemented? – Bista Sep 20 '16 at 15:31
  • @Mr.UB can't find such func. i'm typing to override it, but doesn't show up – anna.O Sep 23 '16 at 13:01
  • Conform to collection datasource and delegates ’class LoginViewController: UITabBarController,UICollectionViewDataSource,UICollectionViewDelegate’ - then this method will show up. – Bista Sep 23 '16 at 13:17
  • @Mr.UB My collection view is in HomeViewController and there i have the protocols and func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int), if you that mean! I think its problem like i don't get templates on the other page in collectionView....really don't know what to do ... – anna.O Sep 28 '16 at 15:50
  • `@IBOutlet` is connected? – Bista Sep 28 '16 at 15:57
  • @IBOutlet collection cell? yes... – anna.O Sep 28 '16 at 21:06
  • No `IBOutlet` of `collection view` and also your code of `numberOfItemssAt...` – Bista Sep 29 '16 at 03:52
  • Everything is conncected! It runs code of HomeViewController where my collection view is, before i login into app. I don't know how it is possible... – anna.O Sep 29 '16 at 09:05
  • show your code of `numberOfItemssAt...` – Bista Sep 29 '16 at 09:41
  • @Mr.UB i edited my question...there is the whole code now :) – anna.O Oct 03 '16 at 14:09
  • put a breakpoint inside `cellForItemAtIndexPath` and see if it is executing or not. – Bista Oct 03 '16 at 16:36
  • It accually report an error on 'return self.templateForCell!.count' . Like it first run the code in HomeVC before LoginVC which is before every VC. I don't why !? – anna.O Oct 03 '16 at 17:58
  • print `templateForCell` in `viewDidLoad` – Bista Oct 03 '16 at 17:59
  • it prints nil ! – anna.O Oct 03 '16 at 18:02
  • what is the output of `print(templates[0])`? – Bista Oct 03 '16 at 18:04
  • Means that you have no data to display. – Bista Oct 03 '16 at 18:07
  • i cant get to ' templates ' because as soon as my login screen loads, the code from HomeViewController runs before every earlier code. And it should login in application first and then get templates from Alamofire, and then to run the code from HomeVC and load collection view... – anna.O Oct 03 '16 at 18:10
  • Try this: `var templateForCell: [VQuizTemplateTo]? = [ ]` – Bista Oct 04 '16 at 04:04

0 Answers0