4

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:

(MyControllerClass*)self.revealViewController.rearViewController
(MyControllerClass*)self.revealViewController.frontViewController

Than I tried things like self.revealViewController.navigationController[0], self.revealViewController.navigationController[1], also this , self.revealViewController.viewControllers[0],self.revealViewController.viewControllers[1]. The next step was to implement prepareforsegue and listen for segue identifier sw_front and sw_rear. 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)?

thanks

user3466562
  • 465
  • 2
  • 10
  • 23

3 Answers3

5

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

user3466562
  • 465
  • 2
  • 10
  • 23
  • Hi, i'm having the same problem identifying the view controller for my menu, do you know how to do this in swift? Could you also post the code for your full segue? Also which segue identifier did you use? Thanks – Ali_bean Nov 08 '15 at 12:42
  • Hi Ali, did you solve your problem ? I am not working with SWIFT, but I could post the Objective C code if it is of any help ? – user3466562 Nov 18 '15 at 21:06
  • i am trying your code but it showing error in if([rearNavigationController.topViewController isKindOfClass[MyCustomViewController]]) – User558 Feb 18 '16 at 09:58
  • But you did change MyCustomViewController to your controller name ? I am using it just as a placeholder ... – user3466562 Mar 23 '16 at 11:46
0

Create a shared object in appdelegate, then you can access it across viewContollers. Create a property in appdelegate say a

@property (nonatomic, strong) NSArray *sharedItem;

Now set this object in your firstViewContoller as:

  AppDelegate *appDel = (AppDelegate*) [UIApplication sharedApplication].delegate;
  appDel.sharedItem = firstControllerObj;

And in your destination VC access the object as:

AppDelegate *appDel = (AppDelegate*) [UIApplication sharedApplication].delegate;
  NSArray *receivingObj = appDel.sharedItem;
Arun Gupta
  • 2,628
  • 1
  • 20
  • 37
  • this is the only way doing it ? I don't think that the best way is making a global variable. It must be possible to somehow access the controllers, otherwise, in my opinion, the SWRevealViewController would be useless – user3466562 Dec 26 '14 at 18:52
0

LoginViewController -> SWRevealViewController -> NavigationController -> HomeViewController (HomeViewController's the frontViewController) That's the sequence of ViewControllers passing through SWRevealViewController

From LoginViewController to SWRevealViewController I use segue. To pass data I use this one in prepareForSegue.

if ([[segue identifier] isEqualToString:@"homeVC"]) {

   SWRevealViewController *destination = [segue destinationViewController];
   [destination loadView];
   UINavigationController *navViewController = (UINavigationController *) [destination frontViewController];

   if ([navViewController.topViewController isKindOfClass:[HomeViewController class]]) {

      HomeViewController* homeVC = (HomeViewController*)navViewController.topViewController;
      homeVC.title = @"Test1";
    } 
}
alitosuner
  • 984
  • 1
  • 10
  • 15