2016-04-25 71 views
0

我有一个表视图。当一个tableview单元格被点击时,im抓取一个特定的文本标签值。在APIRequest.m文件中,我有一个变量shipmentReferenceNo,它的值im在didSelectRowAtIndexPath上指定为textlabel值。无法通过didSelectRowAtIndexPath传递值

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarController"]; 
    [self.navigationController pushViewController:tabBar animated:YES]; 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UILabel *label = (UILabel *)[cell viewWithTag:123123]; 
    self.request = [[APIRequest alloc]init]; 

    self.request.shipmentReferenceNo = label.text; 

    NSLog(@"hello %@", label.text); 

} 

当我尝试从APIRequest.m文件如下如下访问shipmentReferenceNo,其空

NSLog(@"sd %@",self.shipmentReferenceNo); 

可能是什么,虽然我已经在didSelectRowAtIndexPath先前分配用于这项空值可能的原因?

+0

这是因为您正在再次创建新实例。 self.request = [[APIRequest alloc] init];你只是删除它。因为你已经宣布self.request作为一个财产!你也需要寻找self.request.shipmentReferenceNo和不self.shipmentReferenceNo –

+0

我试过了,但仍然即时通讯有相同的问题 – user1241241

+0

在didSelect方法中放置一个断点并尝试在调试器中打印self.request的值。 –

回答

1

试试这个代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 
    UILabel *label = (UILabel *)[cell viewWithTag:123]; 
    self.request = [[APIRequest alloc]init]; 
    self.request.shipmentReferenceNo = label.text; 
    NSLog(@"hello %@", label.text); 

    dispatch_async(dispatch_get_main_queue(), ^{ 
    tabBar = [self.storyboard instantiateViewControllerWithIdentifier:@"TabBarController"]; 
    [self.navigationController pushViewController:tabBar animated:YES]; 
}); 

}

也在里面表视图的数据源:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = ; 
    [cell.label setTag:123]; 
} 

这将工作,但我会建议让你的代码更通过移动dispatch_async组织代码来分离方法。