我正在尝试实现一个幻灯片菜单,当你选择一个选项时,它会向第二个视图控制器发送一个变量值,该控制器有一个来自parse的查询,该查询将根据幻灯片菜单中的选定值进行更新
如何将didSelectRowAtIndexPath中的变量传递给另一个视图控制器
@property (strong, nonatomic)NSString *passVariable;
- (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];
}];
}
NSString *vName = MenuViewViewController.passVariable;
尝试了上述内容,但是passVariable在secondViewController.m
中不起作用答案 0 :(得分:3)
您希望将对象“推送”到下一个viewcontroller,而不是从之前的viewController中提取它们。
在SecondViewController中创建一个@property (copy, nonatomic) NSString *fieldName;
并使用它:
SecondViewController *newTopViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
newTopViewController.fieldName = @"FieldName";
答案 1 :(得分:0)
我看到两种可能性:
1)尝试使用Singleton
方法:(如果可能且必要)
在menuViewController中:
[Singleton sharedInstance].passVariable = @"yourValue here"
在viewDidLoad
下的secondViewController:
someProperty = [Singleton sharedInstance].passVariable;
2)尝试在secondViewController中创建一个属性:
@property (nonatomic, copy) NSString * passVariable;
合成它:
@synthesize passVariable;
现在在menuViewController中写道:
SecondViewController *newTopViewController = (SecondViewController*)[self.storyboard instantiateViewControllerWithIdentifier:identifier];
newTopViewController.passVariable = @"yourValue here";