navigating from uitableviewcell to multiple uinavigationcontroller using swrevealviewcontroller

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?

Upvotes: 3

Views: 2121

Answers (1)

Gurtej Singh
Gurtej Singh

Reputation: 3234

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.

Upvotes: 5

Related Questions