0

I want to create a slidebar menu in my app (As shown in the image below) enter image description here

When I click that hamburger icon, it should show a menu as shown below. enter image description here

I am currently using the SWRevealController, a lib written in Objective C. I created a bridging header and imported the class. This is my current Set up enter image description here

My initial view which branches to a Menu Controller or the UIViewTable is a SWRevealController, and the segues are subclasses of SWRevealViewControllerSegueSetController. In my Food Item View Controller (the controller where the hamburger navigation exist), I added the following code to activate the slidebar action.

override func viewDidLoad() {
        super.viewDidLoad()

        //Creating the slidebar Navigation using SWRevealViewController 
        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }

}

However, when I click the button, it is still not responding. When I did this with my other application, it worked fine. I am not sure why it is not working here

This is the source I used as reference: http://www.appcoda.com/sidebar-menu-swift/

View Controller Full Code

@IBOutlet weak var thumbnail_image: UIImageView!
    //@IBOutlet weak var food_detail: UITextView!
    @IBOutlet weak var testing: UILabel!

    @IBOutlet weak var menuButton: UIBarButtonItem!
    var jsonResponseFood: NSArray = NSArray() //API for types of food available
    var jsonResponseRecipe: NSArray = NSArray() // API for recipes in food
    var selectedFood: foodItem?
    var postStr: String?

    //Initializing API
    let FullRecipeAPIModel = fullRecipeAPIModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        //Creating the slidebar Navigation using SWRevealViewController 
        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }


        print(selectedFood?.name as String!)
        //testing.text = self.selectedFood?.name

        let APIModel = apiModel()

        APIModel.delegate = self
        FullRecipeAPIModel.delegate = self
        APIModel.downloadItems(postString: postStr!)

        // Do any additional setup after loading the view.
    }

Thank you StackOverflow.

cruise_lab
  • 649
  • 8
  • 21

2 Answers2

0

Problem in your code is you are not setting SWRevealController as root view.No Storyboard option available by code wise we have to do configuration once again. This library was designed like that. so if you change code as below it will work like a charm.

if(stat == "login"){
                if(responseString == "invalid"){
                    self.isValid = false;
                    print(self.isValid)
                }

                DispatchQueue.main.async(execute: {
                    if self.checkLogin(data: responseString!) == true {
                        let storyboard = UIStoryboard(name: "food", bundle: nil)
                        let controller = storyboard.instantiateViewController(withIdentifier: "foodItemViewController") as! foodItemViewController
                        let navController = UINavigationController(rootViewController: controller) // Creating a navigation controller with VC1 at the root of the navigation stack.
                        let appDelegate = UIApplication.shared.delegate as! AppDelegate
                        let menuView :MenuController = storyboard.instantiateViewController(withIdentifier: "menuBar") as! MenuController
                        let revealController = SWRevealViewController()

                        revealController.frontViewController = navController;
                        revealController.rearViewController = menuView;

                        appDelegate.window?.rootViewController = revealController

                    }
                    else{
                        //Show alert here
                        self.showAlert(title: "User Does Not Exist", alertTitle: "Close", message: "");
                    }
                });
            }
            else{
                //CODE COULD BE SIMPLIFIED
                DispatchQueue.main.async(execute: {
                    if(responseString == "duplicate"){
                        //Show alert here
                        self.showAlert(title: "User with this email already exist", alertTitle: "Close", message: "Please register with another email address")
                    }
                    else{
                        //Show Food UITable
                        let storyboard = UIStoryboard(name: "food", bundle: nil)
                        let controller = storyboard.instantiateViewController(withIdentifier: "entranceFoodController") 
                        /*let navController = UINavigationController(rootViewController: controller) // Creating a navigation controller with VC1 at the root of the navigation stack.*/
                        let appDelegate = UIApplication.shared.delegate as! AppDelegate

                        let menuView :MenuController = storyboard.instantiateViewController(withIdentifier: "menuBar") as! MenuController
                        let revealController = SWRevealViewController()

                        revealController.frontViewController = controller;
                        revealController.rearViewController = menuView;

                        appDelegate.window?.rootViewController = revealController                    }

                });
            }
Vinodh
  • 5,262
  • 4
  • 38
  • 68
  • So I made the changes as suggested, it is still not working :( – cruise_lab Apr 02 '17 at 05:15
  • Hi Vinodh, I ran into a problem with adding the slidebar menu for my other view, can I ask you another question? – cruise_lab Apr 02 '17 at 14:09
  • Sorry for the extremely late response. Here is my question, https://stackoverflow.com/questions/43554848/sidebar-menu-is-not-working-when-it-is-coming-back-from-the-detail-view-controll I have updated my github code based on my new problem – cruise_lab Apr 22 '17 at 12:09
0

I am also working on this today, is that the best choice available to use SWRevealController, isn't there an Apple built in way of doing this?. I am eager to see options instead of getting something form Github, unless that is my last resort.

Fahd
  • 5
  • 3
  • mhmm i am not sure, but I quite liked SWRevealController, it was quite straightforward. I used it before in another app, and it worked well, but don't know why its not working here – cruise_lab Apr 02 '17 at 13:34
  • Is SWREVEALCONTROLLER the best option, I am working in SWIFT3. – Fahd Apr 12 '17 at 02:19
  • I have not tried any other, but SWREVEALCONTROLLER is one of the easier ones to use, based on experience – cruise_lab Apr 22 '17 at 14:41