【问题标题】:UITableViewController isn't passing dataUITableViewController 没有传递数据
【发布时间】:2013-07-23 22:19:47
【问题描述】:

我试图在 UITableViewController 之间传递数据,显示为:

SeleccionBundesligaViewController *seleccionBundesligaVC = [[SeleccionBundesligaViewController alloc]initWithStyle:UITableViewStylePlain];

seleccionBundesligaVC.title = @"1.Bundesliga";
[self.navigationController pushViewController:seleccionBundesligaVC animated:YES];

当我选择一行时,它被关闭:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [equipos objectAtIndex:indexPath.row];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
    InformacionPartidaViewController *infoPartidaVC = [storyboard instantiateViewControllerWithIdentifier:@"infoPartidaVC"];
    [self.navigationController popViewControllerAnimated:YES];
    [infoPartidaVC.labelNombreEquipo setText:[dic objectForKey:@"nombre"]];

}

但是当我选择该行时,它不会将数据传递给“infoPartidaVC”。

【问题讨论】:

    标签: ios uiviewcontroller uitableview


    【解决方案1】:

    最好的办法是在 SeleccionBundesligaViewController 的 .h 文件中创建一个属性:

    @property (weak, nonatomic) InformacionPartidaViewController *infoPartidaVC;
    

    当你创建你的 UITableViewController 并且在你推送它之前,添加引用:

    SeleccionBundesligaViewController *seleccionBundesligaVC = [[SeleccionBundesligaViewController alloc]initWithStyle:UITableViewStylePlain];
    
    seleccionBundesligaVC.title = @"1.Bundesliga";
    seleccionBundesligaVC.infoPartidaVC = self;
    [self.navigationController pushViewController:seleccionBundesligaVC animated:YES];
    

    然后,在你的 UITableViewController 中的 didSelectRowAtIndexPath 中:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSDictionary *dic = [equipos objectAtIndex:indexPath.row];
    
        [self.navigationController popViewControllerAnimated:YES];
        [self.infoPartidaVC.labelNombreEquipo setText:[dic objectForKey:@"nombre"]];
    }
    

    【讨论】:

      【解决方案2】:

      我认为您的问题是您在显示标签之前设置标签的文本。 看看这个question问我回答了

      【讨论】:

      • 我该如何初始化它?
      • 在 infoPartidaVC 上创建一个 NSString 属性设置为 didSelectRowAtIndexPath 中的值,在 infoPartidaVC 的 viewDidLoad 中使用字符串设置标签
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2018-01-01
      相关资源
      最近更新 更多