Sidemenu: Present View Controller from side menu

Created on 4 Mar 2016  路  17Comments  路  Source: jonkykong/SideMenu

What is the appropriate way to present a view controller from the menu when a row is selected (using TableViewController? Right now I am pushing modally (cover up presentation) based on the selected row, but I'd prefer it to just change whatever controller that was pushed to the side when the menu is displayed and then hide the menu when the row is selected.

Does that make sense?

Most helpful comment

@albernackee I just tested this in the demo project as follows:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "Test")
        navigationController?.pushViewController(vc!, animated: true)
    }

It worked as expected. As such, all I can tell you is you're doing something wrong without you being more thorough in your investigation.

All 17 comments

Hi @dtfiedler -- if you want to do it in code, use didSelectRowAtIndexPath from your menu table view controller to do a pushViewController from self.navigationController just like you would normally. If in storyboard, just change your modal segue on the action for that specific cell over to a push. Check out the demo project for an example of the storyboard version.

you're a lifesaver thank you! if anyone else has an issue here is a snippet of code:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         switch(indexPath.row){
         case 0:
             let homeVC = self.storyboard?.instantiateViewControllerWithIdentifier("MainNavigationController") as! UINavigationController
             self.navigationController?.pushViewController(homeVC.viewControllers.first!, animated: true)
             break
         case 1:
             let eventsVC = self.storyboard?.instantiateViewControllerWithIdentifier("EventsNavigationController") as! UINavigationController
             self.navigationController?.pushViewController(eventsVC.viewControllers.first!, animated: true)
            break
         case 5:
             NSUserDefaults.standardUserDefaults().removeObjectForKey("firstName")
             NSUserDefaults.standardUserDefaults().removeObjectForKey("lastName")
             NSUserDefaults.standardUserDefaults().removeObjectForKey("email")
             NSUserDefaults.standardUserDefaults().removeObjectForKey("password")
             NSUserDefaults.standardUserDefaults().synchronize()
             let signInPage = self.storyboard?.instantiateViewControllerWithIdentifier("SignInNavigationController") as! UINavigationController

             self.navigationController?.pushViewController(signInPage, animated: true)

             break
         default:
             break
         }
     }

I am attempting to push a view controller from the right side bar in code like so:
let vc = ProfileViewController(nibName: nil, bundle: nil, userMin: group.creator) self.navigationController?.pushViewController(vc, animated: true)
but it appears to be presenting modally. I want the new view controller (ProfileViewController) to push onto the view controller stack that presented the menu. Is this possible in the code? I looked at the example but it is all using storyboard. Similar to the examples selection from the left side menu, but when the back button is hit, also keep displaying the menu.

@albernackee Yes, this is possible to do without storyboard. Please review the README for the instructions I've provided there.

@jonkykong I really appreciate the quick reply! I have gone over the README a number of times but I do not see any examples of pushing a view controller from an action in the side bar. I only see how to initially present the side bar. I'm sorry if somehow I'm missing it.

@albernackee I just tested this in the demo project as follows:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = storyboard?.instantiateViewController(withIdentifier: "Test")
        navigationController?.pushViewController(vc!, animated: true)
    }

It worked as expected. As such, all I can tell you is you're doing something wrong without you being more thorough in your investigation.

@jonkykong I don't see much of a difference. It must just be that mine is not a storyboard item :/

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let vc = ProfileViewController(nibName: nil, bundle: nil, userMin: self.groupInfo!.creator)
    self.navigationController?.pushViewController(vc, animated: true)
}

I'll figure something out. Thank you for your time!

@albernackee my best guess is that whatever screen you have the menu appear on doesn't have a have a navigation controller, so SideMenu instead attempts to present it.

@jonkykong Please correct me if I'm wrong, but wouldn't self.navigationController return nil if there was no UINavigationController, and then nothing would happen at all? And the view controller that presents SideMenu is within a UINavigationController.viewControllers stack. If you would like to help, that would be awesome and I can send you some more info. If not, no worries!

@albernackee indeed it would, however the behavior of the library is that you're actually asking the menu to push, but it instead hands it off to the presentingViewController for it as the expected behavior. It has a check for a navigationController in the presentingViewController, and if nil I dummy-proofed it to instead presentViewController.

Now I'm thinking I should just have it do nothing as a more obvious indication of what could be wrong to the developer.

@jonkykong Ahh, that's it! I was running into the issue because my app's rootViewController is a UITabBarController. I was getting your SideMenu Warning but didn't notice it with all my other logs. For anyone else that may have this issue, I changed one line in UISideMenuNavigationController.swift from:
presentingViewController = presentingViewController as? UINavigationController
to:
presentingViewController = (presentingViewController as? UITabBarController)?.selectedViewController as? UINavigationController
Thank you @jonkykong for all your help 馃檹

Hello,
How Can I enable menu for all screen?
I need to avilable sidemenu when I presentview any viewcontroller from side menu.
Thank you,

@Piyush08 have you enable menu for all screen ?
If 'Yes' could you please help me, that how you do that ?

Hi, i have the same problem. I need to enable menu for all screen. Some help please :(

Hi, i have a same problem.

Hi, i have a same problem.

Hi, i have a same problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harshvishu picture harshvishu  路  3Comments

taylorhredding picture taylorhredding  路  3Comments

CaliCastle picture CaliCastle  路  3Comments

selaysoysal picture selaysoysal  路  7Comments

Imtiazaa picture Imtiazaa  路  5Comments