在视图控制器之间传递字典

时间:2012-04-06 14:11:10

标签: objective-c ios memory memory-management properties

我在这里缺少一些基本的东西。

我有一个表视图,每个单元格显示NSDictionary的内容。当我点击每个单元格时,我想要转到一个新的视图控制器,显示来自同一个字典的详细信息。

但每次我尝试传递字典时,其内容在另一方都是空的。

这是我在包含表视图的视图控制器中准备segue:

 -(void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"tableSegue"]){

        MODetailViewController *detailViewController = [[MODetailViewController alloc] init];

        NSIndexPath *selectedIndexPath = [self.monetiseTable indexPathForSelectedRow];
        int selectedIndexPathAsInteger = selectedIndexPath.row;

        NSDictionary *dictionaryToPass = [[NSDictionary alloc] initWithDictionary:[self.feedArray objectAtIndex:selectedIndexPathAsInteger]];

        NSLog(@"%@", dictionaryToPass);

        detailViewController.passedDictionary = dictionaryToPass;
    }
}

NSLog按预期显示字典。

现在,详细视图控制器头我已经声明了属性(我正在使用ARC):

@property (weak, nonatomic) NSDictionary *passedDictionary;

现在在viewWillAppear:

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:YES];

NSLog(@"%@", self.passedDictionary);
}

NSLog返回null!?

我已经合成了它。

我缺少一些基本的东西,我敢肯定。有什么帮助吗?

2 个答案:

答案 0 :(得分:2)

@property (weak, nonatomic) NSDictionary *passedDictionary;

而不是weak,声明strong

这将阻止字典被解除分类。

祝你好运!

答案 1 :(得分:2)

不要分配新的MODetailViewController。使用传递给您的那个作为segue参数中的目标控制器。