mugunthan
mugunthan

Reputation: 77

passing data from didSelectRowAtIndexPath to another view controller ios7

i am trying to implement a slide menu, where when you select an option, it sends a variable value to a second view controller, which has a query from parse, which would get updated based on the selected value from the slide menu.

how do i pass a variable from didSelectRowAtIndexPath to another view controller

menuViewController.h

@property (strong, nonatomic)NSString *passVariable;

menuViewController.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

NSString *identifier = [NSString stringWithFormat:@"%@", [self.menu objectAtIndex:indexPath.row]];

UIViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
    _passVariable = @"FieldName";


[self.slidingViewController anchorTopViewOffScreenTo:ECRight animations:nil onComplete:^{
    CGRect frame = self.slidingViewController.topViewController.view.frame;
    self.slidingViewController.topViewController = newTopViewController;
    self.slidingViewController.topViewController.view.frame = frame;
    [self.slidingViewController resetTopView];
}];

}

SecondViewController.m

   NSString *vName = MenuViewViewController.passVariable;

tried the above, but the passVariable does not work in the secondViewController.m

Upvotes: 1

Views: 2719

Answers (2)

NeverHopeless
NeverHopeless

Reputation: 11233

Two possibilities as I see:

1) Try using Singleton approach: (if possible and necessary)

In menuViewController:

[Singleton sharedInstance].passVariable = @"yourValue here"

In secondViewController under viewDidLoad:

someProperty = [Singleton sharedInstance].passVariable;

2) Try by making a property in secondViewController:

@property (nonatomic, copy) NSString * passVariable;

synthesize it:

@synthesize passVariable;

Now in menuViewController write like:

SecondViewController *newTopViewController = (SecondViewController*)[self.storyboard instantiateViewControllerWithIdentifier:identifier];

newTopViewController.passVariable = @"yourValue here";

Upvotes: 0

Matthias Bauch
Matthias Bauch

Reputation: 90117

You want to "push" objects to the next viewcontroller, not pull them from the previous viewController.

create a @property (copy, nonatomic) NSString *fieldName; in your SecondViewController and use this:

SecondViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
newTopViewController.fieldName = @"FieldName";

Upvotes: 3

Related Questions