Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWRevealViewController passing parameters

Passing parameters from one ViewController to other ViewController through a SWRevealViewController. This is not about passing parameter between Front Controller and Rear Controller, but from another ViewController (firstViewController) to the front (frontViewController. See image for reference. The segue (on button touch) tries to pass a string value to the front controller

image here: http://portwire.com/stackoverflow/storyboard.png

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SWRevealViewController *destinationViewController = (SWRevealViewController *)segue.destinationViewController;
    UINavigationController *navegationController = (UINavigationController *)destinationViewController.frontViewController;

    //UUPS!! SWRevealViewController.frontViewController is nil

    //How to access destination UIViewController????????

    FrontViewController * frontViewController = (FrontViewController *)navegationController.topViewController;
    frontViewController.fromString = @"String from previous view";
}
like image 890
My1 Avatar asked Feb 16 '26 10:02

My1


2 Answers

For Swift 3.0:

  1. You have to named storyBoardId for SWRevealViewController as any thing you want, in this example: "SWRevealViewController"

  2. Follow this code below:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let revealVc = storyboard.instantiateViewController(withIdentifier :"SWRevealViewController") as! SWRevealViewController
    revealVc.loadView()
    let navViewController = revealVc.frontViewController as! UINavigationController
    if (navViewController.topViewController?.isKind(of: MainVc.superclass()!)) {
    let mainVc = navViewController.topViewController as! MainVc
    mainVc.email = email
    }
    self.present(revealVc, animated: true, completion: nil)
    
like image 85
dkan10 Avatar answered Feb 20 '26 02:02

dkan10


I have first LoginViewController -> SWRevealViewController -> NavigationController -> HomeViewController (HomeViewController is the frontViewController)

From LoginViewController to SWRevealViewController I have a segue. To pass data,I use this code in prepareForSegue that works for me.

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

Hope that helps you!

like image 32
alitosuner Avatar answered Feb 20 '26 01:02

alitosuner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!