У меня есть представление UIPicker, для которого мне нужно загрузить данные из фида JSON.
Я могу записать данные массива в NSLOG и отобразить их, но не могу увидеть их в UIPickerview. пожалуйста, посмотрите код ниже и помогите мне
Пожалуйста, дайте мне знать, как мы можем отобразить представление UIPicker, когда я нажимаю кнопку UI и закрываю UIPickerview, когда выбрано значение.
#import "orderSamples.h"
#import "SBJson.h"
#import "JSON.h"
@implementation orderSamples
NSMutableArray *products;
NSArray *list;
NSMutableData *responseData;
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
*/
- (void)viewDidLoad
{
[super viewDidLoad];
products = [[NSMutableArray alloc] init];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http JSON URL"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
list = [[NSMutableArray alloc] init];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
//[responseData release];
NSDictionary *dictionary = [responseString JSONValue];
NSArray *response = [[dictionary valueForKey:@"products"] retain];
list = [response valueForKey:@"product_name"];
NSLog(@"Here is the products list: %@",[response valueForKey:@"product_name"]);
self.pickerData = list;
[list release];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [list count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [list objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSString *str = [list objectAtIndex:row];
NSLog(@"Selected : %@" , str);
}