Home > database >  How to enable SideMenu to work with ViewController functions | Swift/Xcode
How to enable SideMenu to work with ViewController functions | Swift/Xcode

Time:03-31

I'm using a pod called "SideMenu" which creates a menu using a tableView. The menu is embedded in my ViewController, but it doesn't have access to the ViewController's functions. When I tap on a row in the menu, I want it to segue to the proper screen. Unfortunately, the segues originate from the ViewController and not the Menu. I obviously can't use a Delegate since I never segue to the tableView - it's embedded. How can I solve this issue?

class ViewController: UIViewController, UINavigationControllerDelegate {
    var menu: SideMenuNavigationController?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup the menu
        setupMenu()
    }

    func setupMenu() {
        menu = SideMenuNavigationController(rootViewController: MenuListController())
        menu?.setNavigationBarHidden(true, animated: false)
        menu?.presentationStyle = .menuDissolveIn
        SideMenuManager.default.rightMenuNavigationController = menu
        SideMenuManager.default.addPanGestureToPresent(toView: self.view)
    }

    func showProfile() {
        performSegue(withIdentifier: "profileSegue", sender: self)
    }
    
    
    func showHelp() {
        performSegue(withIdentifier: "helpSegue", sender: self)
    }
    
    
    func showSettings() {
        performSegue(withIdentifier: "settingsSegue", sender: self)
    }
    
    
    func showPreferences() {
        performSegue(withIdentifier: "preferencesSegue", sender: self)
    }

}

class MenuListController: UITableViewController {
    
    var items = ["Profile", "Help", "Settings", "Preferences", "Log Out"]
    var darkColor = UIColor(red: 33/255.0, green: 33/255.0, blue: 33/255.0, alpha: 1)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.backgroundColor = darkColor
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        var content = cell.defaultContentConfiguration()
        content.text = items[indexPath.row]
        content.textProperties.color = .white
        cell.contentConfiguration = content
        cell.backgroundColor = darkColor
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if indexPath.row == 0 {
            // Profile
        } else if indexPath.row == 1 {
            // Help
        } else if indexPath.row == 2 {
            // Settings
        } else if indexPath.row == 3 {
            // Preferences
        } else if indexPath.row == 4 {
            // Log Out
            logOut()
        }
        
    }
    
}

I couldn't find the answer in the GitHub documentation for the pod. Here is the link anyway: SideMenu

CodePudding user response:

You can navigate like this also.

 func openSettings(){
        let storyBoard = UIStoryboard(name: "ReplaceStoryBoardName", bundle: nil)
        let childVc = storyBoard.instantiateViewController(withIdentifier: "ReplaceYourIdentifierFromStoryBoard") as? YourViewControllerName
childVc.anyData = self.Data // or whatever data you want to sent you can inject it here.

        self.navigationController?.pushViewController(childVc, animated: true)
    }

There are some others way like NotificationCenter You can post the notification from Sidemenu View Controller Then Observe it on your ViewController in which you have embeded the SideMenu

  • Related