To add interactivity to UITableView
s, UICollectionView
s, and other kinds of views which display collections of data, you can't use Storyboard actions, as the content is generated dynamically during runtime, and the Storyboard can only work for static content.
Instead, what you need to do is set your UICollectionView
's delegate
property to an object that implements the UICollectionViewDelegate
protocol. One of the methods defined as part of the protocol is the collectionView(_:didSelectItemAt:)
method. This method will get called whenever the user selects (taps) a collection view cell with the IndexPath
to that cell as an argument. You can update your variable in that method. Just remember to deselect the cell after handling the tap by using the deselectItem(at:)
method on your UICollectionView
.