-1

I'm currently implementing an App with horizontal collectionView and a navigationBar at the top. The horizontal view are implemented with the page-Mode activated, so the user can select between 4 tabs. Each of the horizontal collectionView has vertical collectionViews in them so the user can also scroll vertically.

Now I want to interact with the user, when he pressed one of the vertical collectionView items. When he pressed an item, a new view should come up without going over the navigationBar at the top. So basically the vertical collection view should be replaced with a different one.

The problem I'm facing is, that I don't know how I can inform my controller that an item is pressed. I know that I can get the selected item when calling didSelectItemAt but how can I implement, that my controller knows that the item is pressed and now has to load a new view also in regards of the MVC model.

Another problem I'm facing is, that I don't know how I can push a new controller that doesn't go over the navigationBar. For this question I have found an answer under this link: Swift 4 - Push a ViewController from a UICollectionView Cell but unfortunally this didn't work for me.

Benipro98
  • 183
  • 2
  • 14

1 Answers1

1

DISCLAIMER: Obviously my example is very simplified, because you didn't post any code. Please consider doing so for the next time, because others can help you better, if you do.


First of all you have to create a Segue from the first to the second ViewController. You can do this by dragging from (1) to (2) with the right mouse button (For testing I used a UITableView, but the whole process should be the same using a CollectionView).

enter image description here

Then you select Show from the context menu.

Now you should have an arrow, pointing from the first to the second ViewController. Select it and give it an unique identifier in the Attribute Inspector to the right.

Now you can go inside your code of the first ViewController and add the following code to the didSelectItemAt function (obviously replacing <YOUR IDENTIFIER> with the actual identifier, you gave to the segue earlier on).

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "<YOUR IDENTIFIER>", sender: collectionView.cellForItem(at: indexPath))
    collectionView.deselectItem(at: indexPath, animated: true)
}

Now if you select a cell, you go to the second ViewController, but the NavigationController remains the same. You even get a back button for free.

If you need to give the second ViewController some information about which Cell was pressed, you have to do a bit more.

Let's assume, you need the IndexPath of the selected cell and your ViewController file of the second ViewController looks like this:

import UIKit

class ViewController2: ViewController {

    @IBOutlet weak var label: UILabel!

    var indexPath: IndexPath!

    override func viewDidLoad() {
        super.viewDidLoad()

        label.text = "\(self.indexPath.row)"
    }
}

Now you need a way to give the IndexPath to the second ViewController. You can do this, using the prepare(for:sender:) function like so:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "<YOUR IDENTIFIER>", let dest = segue.destination as? ViewController2, let cell = sender as? UICollectionViewCell {
        dest.indexPath = self.collectionView.indexPath(for: cell)!
    }
}
Josef Zoller
  • 921
  • 2
  • 8
  • 24
  • Thanks for the answer! I will post some code in the future. In my example I didn t use a Storyboard. Do you know how I can perform your code without a Storyboard. For example how I can set the segue identifier of the View? – Benipro98 Apr 12 '20 at 14:55
  • I think I found the answer here: https://stackoverflow.com/questions/41767655/access-a-uicollectionviews-parent-uiviewcontroller – Benipro98 Apr 12 '20 at 15:41
  • 1
    @Benipro98 You can also do something like [this](https://stackoverflow.com/a/36216660/8162321) – Josef Zoller Apr 12 '20 at 15:45
  • @Benipro98 a question, because I really don’t know. Is there a reason, to not use a Storyboard in 2020? It is so much simpler imho. – Josef Zoller Apr 12 '20 at 15:48
  • I followed the instruction of "lets build that app" on youtube. He never used a storyboard in his videos long ago. Do you think using a storyboard is easier/better than without? – Benipro98 Apr 12 '20 at 16:20
  • 1
    Yes I would use it. It needs a little bit of time to get into it, but then everything is just much easier, especially auto layout. I would recommend you [this very good tutorial site](https://www.raywenderlich.com). – Josef Zoller Apr 13 '20 at 01:44