I have a collection view inside a container view. In collection view I have two parts as, UICollectionReusableView
and UICollectionViewCell
. Inside the UICollectionReusableView again I have a collection view and while selecting a cell from that cell I have to change the LeftNavigationBarTitle
. I tried the below code in didSelectItem of collection view but nothing is happen.
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "cancel-music")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(self.closeViewTapped)).
I used the above code but it didn't change the navigationButton.
This my storyBoard design.
Is any mistake in my code, please help me to get the exact one. Thankyou.
Create the protocol method for view controller which contains collection view.
Inside HomeFilterVC
protocol HomeFilterVCDelegate: class {
func collectionViewDidTapped()
}
Then, declare delegate variable like as follow, which will assign to your view controller which contains UIContainerView
.
class HomeFilterVC: UIViewController {
weak var delegate : HomeFilterVCDelegate?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let selfDelegate = self.delegate {
selfDelegate.collectionViewDidTapped()
}
}
}
Implement prepare(for segue:, sender:)
and HomeFilterVCDelegate
methods in HomeVC
and update your code as follow.
HomeVC:
class HomeVC: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "HomeFilterVC" {
let collectionVC = segue.destination as! HomeFilterVC
collectionVC.delegate = self
}
}
}
extension HomeVC: HomeFilterVCDelegate {
func collectionViewDidTapped() {
navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "cancel-music")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(self.closeViewTapped)).
}
}
I hope this will fix your issue.