2016-04-25 20 views
0

私はテーブルビューを持っています。テーブルビューセルがクリックされると、特定のテキストラベル値を取得します。 APIRequest.mファイルには、変数shipmentReferenceNoがあり、その値imはdidSelectRowAtIndexPathのtextlabel値として割り当てられています。私はdidSelectRowAtIndexPathで以前にそれを割り当てたものの、次のようにAPIRequest.mファイルから次のようにshipmentReferenceNoにアクセスしようとしているイム、そのヌルdidSelectRowAtIndexPathによって値を渡すことができません

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

このNULL値の可能な理由は何ができるか

- (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); 

} 

+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を移動して整理するために提案しますメソッドを分離するコード。

関連する問題