So, basically, I implemented a SideMenu using the module side menu from pod. however, I am stuck at the last part where when I tapped on any of the options inside the menu, the side menu shall dismiss itself and display corresponding content depending on which menu option I picked.
my code for my main view controller is below:
class Profile: UIViewController, MenuControllerDelegate {
var menu: SideMenuNavigationController?
var menuController:MenuListController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
menu = SideMenuNavigationController(rootViewController: MenuListController())
menu?.leftSide = true
menu?.setNavigationBarHidden(true, animated: false)
SideMenuManager.default.leftMenuNavigationController = menu
SideMenuManager.default.addPanGestureToPresent(toView: self.view)
setupNavBaritem()
}
func handleMenuToggle(menuOption: String?) {
print("menuToggle function reached")
menu?.dismiss(animated: true, completion: nil)
}
func setupNavBaritem() {
let menuicon = UIImage(named: "menu")
let menubtn = UIButton(type: .system)
menubtn.setImage(menuicon, for: .normal)
menubtn.frame = CGRect(x: 0, y: 0, width: 35, height: 35)
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: menubtn)
menubtn.addTarget(self, action: #selector(menubtntapped), for: .touchUpInside)
navigationController?.navigationBar.isTranslucent = false
}
@objc func menubtntapped(){
present(menu!,animated:true)
}
}
then this is my menu controller:
class MenuListController:UITableViewController {
var menuitems = ["1","2","3","4"]
var delegate:MenuControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.register(MenuCell.self, forCellReuseIdentifier: "menucell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuitems.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menucell", for: indexPath) as! MenuCell
let menuetext = menuitems[indexPath.row]
let menuicon = menuimg[indexPath.row]
cell.setmenucell(iconimg: menuicon, text: menuetext)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMenu = menuitems[indexPath.row]
tableView.deselectRow(at: indexPath, animated: true)
delegate?.handleMenuToggle(menuOption: selectedMenu)
}
}
and this is my protocol:
protocol MenuControllerDelegate {
func handleMenuToggle(menuOption:String? )
}
New updates! with the delegate setup, i am able to reach the toggle function and print("menuToggle function reached"). but my dismiss doesnt work. i have been searching for a while and seems a lot of ppl are having trouble using the dismiss function to dismiss this sidemenu. any workaround that may work on this? i tried a few, including setting the new root controller, which didnt work
i missed the delegate in this function
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedMenu = menuitems[indexPath.row]
let temp = Profile()
tableView.deselectRow(at: indexPath, animated: true)
self.delegate = temp
delegate?.handleMenuToggle(menuOption: selectedMenu)
}
now, i can print out my menutoggle function!