iOS在推送新视图时传输数据

时间:2012-10-31 19:56:44

标签: ios tableview pushviewcontroller nsobject

我有一个应用程序,它解析XML并存储在NSObject(“Call”)中。然后我将表格显示在如下数据中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row];

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}

cell.textLabel.text = currentCall.call_name;
cell.detailTextLabel.text = currentCall.location;
cell.accessoryType = UIButtonTypeRoundedRect;

return cell;
}

我的问题是当我推送到详细视图控制器时,如何设置didSelectRowAtIndexPAth方法以便能够访问所有对象属性?我使用这种方式,这是有效的,但我相信它不是最有效的方式。谢谢你的帮助。

旧方式:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath = %d", indexPath.row);

Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];

self.currentCallTypeDetail = currentCall.currentCallType;
self.unitsDetail = currentCall.units;
self.locationDetail = currentCall.location;
self.countyDetail = currentCall.county;
self.stationDetail = currentCall.station;
self.fullcallnumberDetail = currentCall.fullcallnumber;
self.latitude = currentCall.latitude;
self.longitude = currentCall.longitude;
self.callTypeDetail = currentCall.callType;
self.callNumberDetail = currentCall.callnumber;
self.timeDetail = currentCall.time;

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
CallTypeDetailController *vc = [storyboard instantiateViewControllerWithIdentifier:@"calltypedetail"];

[vc setCurrentCallTypeDetail:self.currentCallTypeDetail];
[vc setUnitsDetail:self.unitsDetail];
[vc setLocationDetail:self.locationDetail];
[vc setCountyDetail:self.countyDetail];
[vc setStationDetail:self.stationDetail];
[vc setFullcallnumberDetail:self.fullcallnumberDetail];
[vc setCallTypeDetail:self.callTypeDetail];
[vc setCallNumberDetail:self.callNumberDetail];
[vc setTimeDetail:self.timeDetail];
[vc setLatitude:self.latitude];
[vc setLongitude:self.longitude];

[self.navigationController pushViewController:vc animated:YES];

NSLog(@"Selected Call = %@", self.currentCallTypeDetail);
} 

1 个答案:

答案 0 :(得分:0)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"indexPath = %d", indexPath.row);

Call *currentCall = [[xmlParser calls] objectAtIndex:indexPath.row / 2];

[tableView deselectRowAtIndexPath:indexPath animated:YES];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle: nil];
CallTypeDetailController *vc = [storyboard instantiateViewControllerWithIdentifier:@"calltypedetail"];

[vc setCurrentCall:currentCall];

[self.navigationController pushViewController:vc animated:YES];

NSLog(@"Selected Call = %@", self.currentCallTypeDetail);
}