2013-02-28 2 views
-1

Я использую AFnetworking для загрузки места в первой точки зрения моего приложения IOSзначение в GetPath URL

LocationTableViewController.m 

[[LocationApiClient sharedInstance] getPath:@"locations.json" parameters:nil 
            success:^(AFHTTPRequestOperation *operation, id response) { 
             NSLog(@"Response: %@", response); 
             NSMutableArray *results = [NSMutableArray array]; 
             for (id locationDictionary in response) { 
              Location *location = [[Location alloc] initWithDictionary:locationDictionary]; 
              [results addObject:location]; 

             } 
             self.results = results; 
             [self.tableView reloadData]; 
            } 
            failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
             NSLog(@"Error fetching locations!"); 
             NSLog(@"%@", error); 

            }]; 

Ответ от АФИ

{ 

     "created_at" = "2013-02-23T19:17:37Z"; 
     id = 1; 
     lat = "51.463842"; 
     long = "-0.6504559999999628"; 
     name = Legoland; 
     "street_address" = "Winkfield Road, Windsor, United Kingdom"; 
     "updated_at" = "2013-02-23T19:17:37Z"; 
    }, 

отобразить имя и street_address

enter image description here

Когда пользователь нажимает на местоположение, приложение iOS segues к BeerTableViewController. Я хотел бы получить щелчок по списку пива мест, используя что-то вроде этого запроса.

BeerTableViewController.m 

NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json", 
    [dictionary objectForKey:@"location_id"]]; 


    [[LocationApiClient sharedInstance] getPath:path parameters:nil 
             success:^(AFHTTPRequestOperation *operation, id response) { 
              NSLog(@"Response: %@", response); 
              NSMutableArray *results = [NSMutableArray array]; 
              for (id beerDictionary in response) { 
               Beer *beer = [[Beer alloc] initWithDictionary:beerDictionary]; 
               [results addObject:beer]; 

              } 
              self.results = results; 
              [self.tableView reloadData]; 
             } 
             failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
              NSLog(@"Error fetching beers!"); 
              NSLog(@"%@", error); 

             }]; 

Я не знаю, как передать LOCATION_ID значение из ячейки LocationTableViewController, когда пользователь нажимает на нее так, что она заполняет URL GetPath с соответствующим значением. При нажатии на месте 1 будет установить URL GetPath в BeerTableViewController для @"locations/1/beers.json

Отклик я вернусь из этого запроса

{ 
     "created_at" = "2013-02-23T19:27:10Z"; 
     description = Awesome; 
     id = 2; 
     "location_id" = 1; 
     name = Pliny the Elder; 
     price = "3.50"; 
     style = IPA; 
     "updated_at" = "2013-02-23T19:27:10Z"; 
    } 

Прямо сейчас я просто получаю ошибки Unknown receiver 'dictionary' No known class method for selector 'objectForKey' в моей просьбе BeerTableViewController.m

NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json", 
     [dictionary objectForKey:@"location_id"]]; 

Должен ли я устанавливать это в

LocationTableViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
+0

Я думаю, что это говорит вам, что не может найти что-нибудь по имени «словаря» в рамках метода. –

ответ

1

Как насчет:

NSString *path = [NSString stringWithFormat:@"locations/%@/beers.json", 
    [dictionary objectForKey:@"location_id"]]; 

[[LocationApiClient sharedInstance] getPath:path //.... 
+0

Когда я добавил, что у меня есть несколько ошибок, неизвестный словарь-ресивер и нет известного метода класса для текущего кода selectorKeyType, вставленного в следующий комментарий. – jacobt

+0

NSString * path = [NSString stringWithFormat: @ "locations/%@/beers.json", [словарь objectForKey: @ "location_id"]]; [[LocationApiClient sharedInstance] Параметры getPath: @ "locations/@location_id/beers.json": nil – jacobt

+1

@JacobTimm Я думаю, что @danh был немного смущен; его ответ использовал словарь, поскольку он мог быть передан вашему методу '-initWithDictionary:' в 'Location.m'. У вас есть экземпляр «Местоположение», доступный в области, в которой вы вызываете метод getPath? Если вы это сделаете (и предположим, что это называется 'myLocation'), просто замените' [myLocation location_id] 'на' [dictionary objectForKey: @ "location_id"] ', и вам должно быть хорошо идти. – trudyscousin

Смежные вопросы