user3466562 user3466562 - 10 months ago 77
iOS Question

iOS SWRevealViewController - pass data between controllers

I am using SWRevealViewController to implement a slide menu (from the left side) in my application. Everything is working fine but now I am facing a little problem.I want to pass data from my "main" view controller (the one that is full visible, and I am not calling it "frontViewController" because it seems like it is not the front one). I have tried all possiblities I can think of to access the slide menu controller and pass data to it. Here is what i have tried:


Than I tried things like
, also this ,
. The next step was to implement
and listen for segue identifier
. But in non of these cases, I got true when checking
isKindOfClass[MyControllerclass class]
. I assume this is because all of these are of type SWRevealViewController, aren't they ? And, prepareForSegue isnt't being called at all, but everything is showing properly

The main question for me is, how can I pass data between my side menu and my "main" (aka front) controller (the other way would also be fine)?


Answer Source

I got the answer. You cannot directly access your own controllers through SWRevealViewController (since it is connected to NavigationController). First you get the navigationController you want and through them you access your controllers (and pass data). Here is the code to the theory:

UINavigationController* rearNavigationController = (UINavigationController*)self.revealViewController.rearViewController; 
//here you get the navigationController which is connected to the controller you want, in my case i want the rear controller

if ([rearNavigationController.topViewController isKindOfClass[MyCustomViewController]]) {
    MyCustomViewController* iWantYouController = (MyCustomViewController*)rearNavigationController.topViewController;

And thats it. Now you can access (and set) every property on your custom controller