How to pass data from AKSwiftSlideMenu menu to a UIViewController in Swift?

395 views Asked by At

Hey guys I am using AKSwiftSlideMenu for slide menu functionality in project and I wonder if is there a way so that I can change the destination ViewController title when I segue to it from the Menu? I tried this code but is not passing the data to the destination:

func slideMenuItemSelectedAtIndex(index: Int32) {
let topViewController : UIViewController = self.navigationController!.topViewController!
print("View Controller is : \(topViewController) \n", terminator: "")
switch(index){
case 0:
    print("Note\n", terminator: "")
    var nextVC = AddSubcategoryVC()
    nextVC.categoryName = "Note"
    self.openViewControllerBasedOnIdentifier("AddSubcategoryVC") 
    print("On menu the cat is: \(nextVC.categoryName)")
    break
default:
    print("default\n", terminator: "")
} 
}

On the destination viewcontroller I have this variable that is to get the data from the Menu:

var categoryName = ""

and it is returning 'nil'. I also tried doing this:

var categoryName : String!

and this:

var categoryName = String()

all returning 'nil'.

1

There are 1 answers

0
Ashish Kakkad On

You have created new object of view controller.

Replace this code :

var nextVC = AddSubcategoryVC()
nextVC.categoryName = "Note"
self.openViewControllerBasedOnIdentifier("AddSubcategoryVC") 

with

let destViewController = self.storyboard!.instantiateViewControllerWithIdentifier("AddSubcategoryVC") as! AddSubcategoryVC
destViewController.categoryName = "Note"// You have to pass your data with view controller witch will open
let topViewController : UIViewController = self.navigationController!.topViewController!

if (topViewController.restorationIdentifier! == destViewController.restorationIdentifier!){
    print("Same VC")
} else {
    self.navigationController!.pushViewController(destViewController, animated: true)
}