iOS SWRevealViewController - pass data between controllers

倖福魔咒の 提交于 2019-12-06 04:26:49

问题


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


回答1:


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




回答2:


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;



回答3:


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";
    } 
}


来源:https://stackoverflow.com/questions/27659343/ios-swrevealviewcontroller-pass-data-between-controllers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!