3

I am new to swift language and start to creating stub application having sliding menu. I used tutorial from http://www.appcoda.com/sidebar-menu-swift/ to create slide menu but want to create application which is dynamic not static as shown in example. I am facing problem in creating segue or doing navigation from the uitableviewcell to the respective uiviewcontrollers which is connected to respective uinavigationcontroller.

following is the code for sliding menu class:

MenuController.swift

import UIKit

class MenuController:UITableViewController
{
    let menuControlList = ["Category 1", "Category 2", "Category 3", "Category 4"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Return the number of rows in the section.
        return menuControlList.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! MenuTableCell

        let row  = indexPath.row
        cell.menuCellText.text = menuControlList[row]
        return cell
    }

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("row clicked :: ", indexPath.row)

        switch indexPath.row
        {
        case 0:
            var cat1View = Category1(nibName:"Category1", bundle:nil)
            self.navigationController?.pushViewController(cat1View, animated: true)
            print(indexPath.row)
            break

        case 1:
            var cat2View = Category2(nibName:"Category2", bundle:nil)
            self.navigationController?.pushViewController(cat2View, animated: true)
            print(indexPath.row)
            break

        case 3:
            var cat3View = Category3(nibName:"Category3", bundle:nil)
            self.navigationController?.pushViewController(cat3View, animated: true)
            print(indexPath.row)
            break

        case 4:
            var cat4View = Category4(nibName:"Category4", bundle:nil)
            self.navigationController?.pushViewController(cat4View, animated: true)
            print(indexPath.row)
            break

        default:
            return;
        }
    }
}

Following is the screenshot of my storyboard:

enter image description here

if i am doing any mistake in creating this please let me know and help me rectify it.

Following is the code of my category 1 class :

class Category1 :UIViewController
{

    @IBOutlet var menuBtn: UIBarButtonItem!


    override func viewDidLoad() {
        super.viewDidLoad()

        if self.revealViewController() != nil {
            menuBtn.target = self.revealViewController()
            menuBtn.action = "revealToggle:"
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }   
}

EDIT:

Tried following solution:

let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
presentViewController(vc, animated: true, completion: nil)

the above code directly opens up category 1 viewcontroller with slideup animation but it is not opening through the navigationcontroller attached with the respective viewcontroller.

if i use the following code:

let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1
self.revealViewController().setFrontViewController(vc, animated: true)

the above code also loads viewcontroller but the sliding menu doesnt slide back in?

  • 1
    It seems like you have defined the interfaces with storyboard but then again, you load them from nib file also. Do you think that this would be needed Category1(nibName:"Category1", bundle:nil). – Sandeep Aug 24 '15 at 15:07
  • 1
    @GeneratorOfOne is correct. You would not need to load the view controllers via nib files. Instead give them storyboard ID's and load them that way, and I do not think you need to do self.navigationController.pushViewController, instead set the `frontViewController` of the `SWRevealViewController` like this (obj-c code) : `[self.revealViewController setFrontViewController:newController];` Let me know if you need a detailed example in Objective-C. Sorry I don't know the syntax in Swift. – Gurtej Singh Aug 26 '15 at 07:23
  • @GurtejSingh please help me out with example in objective-C so i can come to know where i am making silly mistake. I have updated my category1 class code also – Niranjan Balkrishna Prajapati Aug 28 '15 at 06:41
  • @NiranjanBalkrishnaPrajapati sure give me some time and I'll answer the question. – Gurtej Singh Aug 28 '15 at 06:45
  • @GurtejSingh i have tried what you and GeneratorOfOne Suggested here is my code 'let vc = storyboard.instantiateViewControllerWithIdentifier("Category1") as! Category1 self.revealViewController().setFrontViewController(vc, animated: true)' but it doesnt work through navigationviewcontroller and i think it directly opens up the category1 screen – Niranjan Balkrishna Prajapati Aug 28 '15 at 06:50
  • @NiranjanBalkrishnaPrajapati Ok, you are on the right track buddy, see my answer below and let me know if it helps. – Gurtej Singh Aug 28 '15 at 07:01

1 Answers1

5

First of all, give your Navigation View Controllers a storyboard ID, and not the actual view controllers. So for example, if Category1 is embedded in the Navigation Controller, then give the Navigation Controller a storyboard ID of Category1NavController for example. Then your code should be as follows:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    print("row clicked :: ", indexPath.row)

    switch indexPath.row
    {
    case 0:
        let navController1 = storyboard.instantiateViewControllerWithIdentifier("Category1NavController") as! UINavigationController 
        self.revealViewController().setFrontViewController(navController1, animated: true)
        print(indexPath.row)
        break
    ..........
    // Handle other cases similarly here
    case 1:
    .....
    case 2:
    .....   
    default:
        return;
    }
    self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)
}

You should also reset the Top View position after your switch statement ends. Here's how it should be (sorry again if syntax is not correct, please fix accordingly):

self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)

There must be a similar method to be called in Swift.

Hope this helps. Please pardon my SWIFT syntax and correct it if needed.

Gurtej Singh
  • 3,244
  • 1
  • 14
  • 27
  • implemented you code the viewcontrollers change on the category click but the slide menu doesnt slide back on the category click. Am I missing any code to slide it back on the menu click on the toggle button ie uibarbutton click the menu slides back due to the following code: "if self.revealViewController() != nil { menuBtn.target = self.revealViewController() menuBtn.action = "revealToggle:" self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer()) }" – Niranjan Balkrishna Prajapati Aug 28 '15 at 07:17
  • 1
    @NiranjanBalkrishnaPrajapati are you calling this line: `self.revealViewController().setFrontViewPosition(FrontViewPosition.Left, animated: true)` ? See my updated code above. Let me know if it works. – Gurtej Singh Aug 28 '15 at 07:30
  • Thank You your solution worked and the swift syntax is also proper I would love to learn objective-c & swift from you will you help me with tutorials or videos to learn It would be grateful of you. Also will you be my friend so i can ask for guidance whenever i am stuck please let me know your fb id so i can add you. – Niranjan Balkrishna Prajapati Aug 28 '15 at 07:33
  • @NiranjanBalkrishnaPrajapati no problem buddy. I'm learning much myself. I'll surely try to help where I can :). Cheers – Gurtej Singh Aug 28 '15 at 07:47