将JSON数据传递给另一个View

时间:2014-05-22 17:04:12

标签: uitableview ios7 jsonmodel

我有一个从Web加载JSON内容的TableView。 我使用AFNetworking和JSONModel。我使用本教程做接收和解析数据 这是代码。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identifier = @"CellIdentifier";
        __weak ProgramacaoTableCell *cell = (ProgramacaoTableCell *)[self.tableView dequeueReusableCellWithIdentifier:identifier];

        ProgramacaoModel* programacao = _programacao.programacaoArray[indexPath.row];

        // NameLabel is the Label in the Cell.
        cell.nameLabel.text = [NSString stringWithFormat:@"%@", programacao.atracao ];

        return cell;

    }

我想知道如何将这些数据传递给Detail ViewController。 在我的DetailViewController中,我有接收数据的属性。

@property (nonatomic, strong) IBOutlet UILabel *programacaoNomeLabel;

2 个答案:

答案 0 :(得分:0)

您可以通过导航访问控制器:

NSArray* vcStack=[self appDelegate].myNavigationController.viewControllers;
UIViewController* vcUnder;
if(vcStack.count > 0)
    vcUnder=[vcStack objectAtIndex:(vcStack.count-1)];
// -1 depends when you called your controller that's why we test the kind of class
if([vcUnder isKindOfClass:[DetailViewController class]]){
    ((DetailViewController*) vcUnder). programacaoNomeLabel = @"some data";
}

答案 1 :(得分:0)

我找到了答案

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Pass the selected object to the new view controller.

    if ([[segue identifier] isEqualToString:@"pushDetalhesView"]) {

        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];

        // Pega o objeto da linha selecionada
        ProgramacaoModel *object = [_programacao.programacaoArray objectAtIndex:indexPath.row];
        // Pass the content from object to destinationViewController
        [[segue destinationViewController] getProgramacao:object];

    }

}

在我的详细信息ViewController中,我创建了一个iVar

@interface ProgramacaoDetalhesViewController ()
{
    ProgramacaoModel *_programacao;
}

并设置两个方法,一个用于接收内容,另一个用于设置标签

- (void) getProgramacao:(id)programacaoObject;
{
    _programacao = programacaoObject;
}

- (void) setLabels
{
    programacaoNomeLabel.text = _programacao.atracao;

}