【问题标题】:Pushing data from one UIViewController to another [duplicate]将数据从一个 UIViewController 推送到另一个 [重复]
【发布时间】:2013-05-24 21:48:43
【问题描述】:

到目前为止,我正在将一个字符串从一个 UIViewController 推送到另一个,但是,当我将推送的字符串添加到 NSMutableArray 和 NSLog 时,它总是返回我的数组计数 0 这是我的代码:我是使用情节提要,我是否需要将第二个视图引用到加载的视图中?

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [_TableView setDelegate:self];
    //[_TableView setDataSource:self];
    _numberOfRows = [[NSMutableArray alloc]init];
}

-(void)pushName:(NSString*)contactName
{
    //NSLog(@"Pushed name is: %@ ", contactName);
    _contactName = contactName;
    NSLog(@"Pushed name is: %@ ", _contactName);//<-- Returns the pushed string fine

    [_numberOfRows addObject:contactName];
    //[self.TableView reloadData];
     NSLog(@"Number of names: %d ", [_numberOfRows count]);//<--Always returns a 0

} 

不确定为什么这不起作用。如果需要更多代码来澄清这个问题,请询问。

【问题讨论】:

  • @Undo 不,这不是一个重复的问题我没有传递一个 NSMutableArray 我只是传递一个字符串我只想在第二个 UIViewController 中填充一个数组它们是完全不同的问题兄弟
  • @one 原理相同。任何传递数组的东西也可以传递字符串。只需浏览答案并将NSMutableArray 替换为NSString
  • @Undo 如果您查看我的代码,我正在传递我的字符串并且它有效,但我的问题是使用传递的字符串填充 NSMutable 数组。尽管我正在查看另一个问题,但根据我的要求,它完全不同。我已经设法为 iPad 做到了,但事实证明这很棘手。如果你能取消我的反对票,我将不胜感激,兄弟
  • Pushed name log 的响应是什么?

标签: iphone ios uitableview nsmutablearray


【解决方案1】:

当您调用 pushName 时,您的数组似乎尚未实例化。

您能否向我们提供您所称的上下文?

编辑:只是为了详细说明。您的问题不在于在两个控制器之间传递数据。您的问题似乎是您的 Array 没有响应 addObject 和 count 消息。

尝试在您的数组上使用惰性实例化,它应该可以工作。

编辑 2:代码 sn-p: 尝试添加方法:

- (NSMutableArray *)numberOfRows:
{
  if( _numberOfRows == nil ) _numberOfRows = [[NSMutableArray alloc] init]
  return _numberOfRows
}

在你使用_numberOfRows的地方尝试使用self.numberOfRows,即

[self.numberOfRows addObject:contactName];
//[self.TableView reloadData];
NSLog(@"Number of names: %d ", [self.numberOfRows count]);//<--Should return 1 or more now.

编辑 3:哦,如果您这样做,您可以删除 viewDidLoad 上的实例化。但你知道的。

编辑 4:修复代码 sn-ps。对不起,我编辑了这么多。只是想确保你得到正确的答案。

【讨论】:

  • 非常感谢,这正是我想要的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-04
相关资源
最近更新 更多