0

I'm an XCode newbie and I'm trying to achieve what follows with an iOS8 app:

  1. create a Tabbed Application
  2. with a single initial tab that contains a text field and a button to:

    1. create a new CollectionView
    2. and add a new tab item to reach it
  3. be able to repeat the creation n times

Steps 1 and 2 are pretty easy. I'm stucked @ point 3. Now in my project I have:

  • MyCollectionViewController which is a default UICollectionViewController where I've changed numberOfSectionsInCollectionView to return 1 section and numberOfItemsInSection to get 3 cells (the reuseIdentifier is set to "MyCell")
  • FirstViewController.swift with:

    • an IBOutlet to a UITextField (newTabItemName) to get the title to use for the new TabItem
    • the following IBAction for the UIButton:

      @IBAction func generateNewTabItem(sender: UIButton) {
          let vc: MyCollectionViewController = MyCollectionViewController()
          vc.title = newTabItemName.text
          self.tabBarController?.viewControllers?.append(vc)
      }
      

If I run the app I see the initial view with the text field and the button; if I change the text and click the button I get the TabItem. I can also do that n times as I wanted. The problem is that if I click one of the new tabs I get the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

I think I'm not that far from what I need but I don't know what's missing.

Any suggestion?

Thanks.

Bye...

TShirt
  • 3
  • 3

1 Answers1

0

As the error is stating, you cannot initialise a UICollectionView without a layout.

See the documentation for initWithFrame:collectionViewLayout:, highlighted by myself:

The layout object to use for organizing items. The collection view stores a strong reference to the specified object. Must not be nil.

UIKit does provide a default collection view layout in the form of UICollectionViewFlowLayout. If you want to do anything more complex you will have to create a custom subclass of UICollectionViewLayout.

Wherever you are creating the UICollectionView (presumably within MyCollectionViewController) would need to be updated to specify such a layout.

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66