【问题标题】:passing data from didSelectRowAtIndexPath to another view controller ios7将数据从 didSelectRowAtIndexPath 传递到另一个视图控制器 ios7
【发布时间】:2014-02-06 07:59:00
【问题描述】:

我正在尝试实现一个幻灯片菜单,当您选择一个选项时,它会向第二个视图控制器发送一个变量值,该控制器具有来自解析的查询,该查询将根据幻灯片菜单中的选定值进行更新.

如何将变量从 didSelectRowAtIndexPath 传递到另一个视图控制器

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;

上面试过了,但是passVariable在secondViewController.m中不起作用

【问题讨论】:

  • 需要明确的是,这个 'didSelectRowAtIndexPath' 方法是否在 MenuViewController 中?另外,这个'newTopViewController'是'SecondViewController'吗?
  • 是的,它在菜单 vie 控制器中,而 newTopViewController 不是第二个视图控制器

标签: ios ios7 uitableview didselectrowatindexpath


【解决方案1】:

您希望将对象“推送”到下一个视图控制器,而不是从前一个视图控制器中拉出它们。

在您的 SecondViewController 中创建一个@property (copy, nonatomic) NSString *fieldName; 并使用它:

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

【讨论】:

  • 我必须声明一个标识符,得到错误“使用未声明的标识符”
【解决方案2】:

我看到的两种可能性:

1) 尝试使用Singleton 方法:(如果可能和必要)

在菜单视图控制器中:

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

【讨论】:

  • 在第二个选项上,passVariable 没有显示为选项,一直说没有什么叫做 pass 变量
  • 转换到合适的控制器实例
  • 我在 .h 文件中添加了它,但是当我使用点语法添加视图控制器名称时它仍然没有显示为选项
  • 我需要将变量从菜单视图控制器传递到第二个视图控制器
  • 确保它被声明为propertysynthesize
猜你喜欢
  • 2022-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
相关资源
最近更新 更多