How to Give Xib Cell Button Action in Homevc in Swift

Changing navigation Left Bar button title while selecting collection cell inside a container view

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.

how to open 'LGSideMenuController' side menu open from all viewcontroller?

You can use the same method to open "LGSideMenuController" as you did in "HomeViewcontroller".

Suppose in another viewcontroller, you want to open "LGSideMenuController" on click of button event then code will look like

@IBAction func openSideMenu(_ sender: Any) {
self.sideMenuController?.showLeftView(animated: true, completionHandler: nil)
}

Edit Answer

I checked your code and found that you are trying to present another viewcontroller screen over "LGSideMenuController" instead of using push transition.

Please change your code in SideMenuVC controller as below

@IBAction func eventsAction(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "EventsVC") as! EventsVC
//let navigationController = UINavigationController(rootViewController: vc)
//self.present(navigationController, animated: true, completion: nil)
self.sideMenuController?.hideLeftViewAnimated()
self.sideMenuController?.rootViewController?.show(vc, sender: self)
}

In the above code, i hide the sidemenu first and then push "EventsVC" view controller in sideMenuController. Please apply the same code on other button actions.

Hope it helps.

How to check if an array index exists or not in javascript?

Use typeof arrayName[index] === 'undefined'

i.e.

if(typeof arrayName[index] === 'undefined') {
// does not exist
}
else {
// does exist
}


Related Topics



Leave a reply



Submit