2016-04-25 76 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組織代碼來分離方法。