In my swift
app I created class that delegates UICollectionViewController
. Additionally, I have other class responsible for handling 'UICollectionReusableView`.
So in the first class I have a method:
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "Header", for: indexPath) as! UserProfileHeaderCollectionReusableView
and thanks to this - in this method - I have access to all buttons and labels stored in the header view, e.g.:
headerView.followButton.isHidden = false
headerView.followButton.addGestureRecognizer(
UITapGestureRecognizer(target: self, action: #selector(followThisUser)))
later on, I have a method followThisUser
:
@objc private func followThisUser(tapGestureRecognizer: UITapGestureRecognizer) {
if (!doIFollow) {
followUser(userId)
} else {
unfollowUser(userId)
}
}
and based on the flag doIFollow
I'm performing specific method.
I would like to give user a feedback when he presses a button and change it's color as soon as he presses it. I tried to access this button by adding:
let tapLocation = tapGestureRecognizer.location(in: self.userProfileCollectionView)
let indexPath : NSIndexPath = self.userProfileCollectionView.indexPathForItem(at: tapLocation)! as NSIndexPath
to the followThisUser
method, but it throws error:
fatal error: unexpectedly found nil while unwrapping an Optional value
How can I access the followButton
then?