Edward Hasted
Edward Hasted

Reputation: 3433

SWReveal TableViewController - how to launch to different ViewControllers?

I'm using the SWReveal slide out menuing system. The menu is generated from an array, and all works fine. E.g.

arrayOfCellData = [cellData(cell : 1, text : "Angles", image : #imageLiteral(resourceName: "Angles.png")),
                   cellData(cell : 2, text : "Area", image : #imageLiteral(resourceName: "Area.png")),

When I tap on a particular menu option I want to segue to a different ViewController. Here is the code that I have been using which doesn't work. The idea that Row 1 invokes segue A and 2 segue B...

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    if(indexPath.row == 1 ){
        let vc: AnyObject! = self.storyboard?.instantiateViewController(withIdentifier: "A")
        self.performSegue(withIdentifier: "A", sender: self)
        self.show(vc as! A, sender: vc)
        NSLog("A")
    }
    if(indexPath.row == 2 ){
        let vc: AnyObject! = self.storyboard?.instantiateViewController(withIdentifier: "B")
        self.performSegue(withIdentifier: "B", sender: self)
        self.show(vc as! B, sender: vc)
        NSLog("B")
    }
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = " "
    navigationItem.backBarButtonItem = backItem


    if segue.identifier == "A", let nextScene = segue.destination as? A {
        nextScene.categoryCounter = 1
    }
    if segue.identifier == "B", let nextScene = segue.destination as? B {
        nextScene.categoryCounter = 2
    }

}

Upvotes: 0

Views: 42

Answers (1)

Brandon A
Brandon A

Reputation: 8289

With SWRevealViewController it is best to not use segues in storyboard and instead contract your reveal view controller programmatically. Try this, in your didSelectRowAtIndexPath method add the following to your code.

// Here I get the view controllers I am interested in
let frontStoryboard = UIStoryboard(name: "YourStoryboard", bundle: .main)
let frontVC = frontStoryboard.instantiateInitialViewController()

let rearStoryboard = UIStoryboard(name: "YourOtherStoryboard", bundle: .main)
let rearVC = rearStoryboard.instantiateInitialViewController()

// Construct the SWRevealViewController with your view controllers
if let revealVC = SWRevealViewController(rearViewController: rearVC, frontViewController: frontVC) {
    revealVC.modalTransitionStyle = .crossDissolve // Set segue transition
    present(revealVC, animated: true, completion: nil) // Segue to view controller
}

Upvotes: 1

Related Questions