我使用 prepareForSegue 方法将 location_id 从 LocationTableViewController 传递到 BeerTableViewController 以在点击位置单元格时获取位置啤酒结果。我的问题是 location_id 每隔一段时间才更新一次。
示例:当点击位置 1 时,它会加载该位置的啤酒。然后我点击后退按钮并点击位置 2,但它仍然加载位置 1 啤酒。如果我再次回击,然后再次点击位置 2,它会显示正确的位置 2 啤酒。
为什么需要两次尝试才能加载正确的 location_id?
LocationTableViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.selected_location = indexPath.row;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:@"beer"]){
BeerTableViewController *beer = (BeerTableViewController *)[segue destinationViewController];
Location *location = [self.location_results objectAtIndex:self.selected_location];
beer.location_id = location.location_id;
}
}
然后在 BeerTableViewController 我使用 location_id 来获取结果
BeerTableViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json", self.location_id];
[[LocationApiClient sharedInstance] getPath:path parameters:nil
success:^(AFHTTPRequestOperation *operation, id response) {
NSLog(@"Response: %@", response);
NSMutableArray *beer_results = [NSMutableArray array];
for (id beerDictionary in response) {
Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary];
[beer_results addObject:beer];
}
self.beer_results = beer_results;
[self.tableView reloadData];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error fetching beers!");
NSLog(@"%@", error);
}];
}