iOS SWRevealViewController - pass data between controllers

僤鯓⒐⒋嵵緔 提交于 2019-12-04 12:54:12

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

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;

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