Reputation: 199
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";
}
Upvotes: 3
Views: 2011
Reputation: 41
For Swift 3.0:
You have to named storyBoardId for SWRevealViewController as any thing you want, in this example: "SWRevealViewController"
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)
Upvotes: 4
Reputation: 1034
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!
Upvotes: 1
Reputation: 760
Using Swift you can do it by this way:
First of all you could create a segue beetween a UIButton
and a ViewController
, to do it you should use your storyboard, choose your element and with Ctrl
drag to your view .
A pop-up will appear, so after that you have to choose Custom
And select the new segue on your Main.storyboard
Identifier
and a Class
with SWRevealViewControllerSeguePushController
Finally you add your param like this in your view controller.
class NewViewController: UIViewController {
var myParam: String!
}
And send the param by this way:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "showMyNewView") {
let detailScene = segue.destinationViewController as! NewViewController
detailScene.myParam = "ParamToSend"
}
}
Hope that helps,
Upvotes: 0
Reputation: 2181
I hope this might help you......
let revealViewCOntroller = segue.destinationViewController as! SWRevealViewController
revealViewCOntroller.loadView()
let secondVc = revealViewCOntroller.frontViewController as! SecondVC
secondVc.loadView()
secondVc.strToken = "messageToPass"
Upvotes: 0
Reputation: 1208
If you set up your front view controller in the storyboard, it is not getting assigned in SWRevealViewController until loadView is called. Just add
[destinationViewController loadView];
Before accessing destinationViewController.frontViewController
Upvotes: 0
Reputation: 1218
I think you may use delegate to do that.
Define a protocol in your front view controller and let the first view controller to conform to it.
Upvotes: 0
Reputation: 686
First of all you need to declare a @property for the NSString in your frontview.
Than in segue method:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showDetails"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *dvc = segue.destinationViewController;
dvc.str = @"Your current view's string";
}
}
Upvotes: -1