我让我的程序工作的方式是它通过 AFHTTPClient 将数据下拉到程序中。然后我得到我的响应数据。然后我获取这些数据并通过 NSJSONSerialization 将其解析为 NSMutableArray。该数组用于填充 UIPickerView。
当用户打开应用程序或按下刷新按钮时会触发此方法。问题是当应用程序打开时,拨打电话,我取回数据。如果我去选择器,它似乎是空的,但是当你向下滚动时,底部的 5 个中有 2 个在那里,当你向上滚动时,其他人也进来了。如果我在任何时候按下刷新按钮,错误就会消失并且选择器已正确填充。为什么它不能正常工作?每次调用后我都会重新加载AllComponents。
-(void) getData{
// Paramaters
NSString *lat = @"40.435615";
NSString *lng = @"-79.987872";
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: lat, @"lat", lng, @"lng", nil];
// posting the data and getting the response
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://mysite.com/"]];
[client postPath:@"/mypostpath.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSString *text = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"Response: %@", text);
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:&error];
// setting the first location to the text feild
NSDictionary* name = [json objectAtIndex:0];
locationField.text = [name objectForKey:@"name"];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"%@", [error localizedDescription]);
}];
// reload the picker
[pickerCust reloadAllComponents];
// setting the first location to the text feild
NSDictionary* name = [json objectAtIndex:0];
locationField.text = [name objectForKey:@"name"];
}