UIPicker 视图不显示 JSON 数组中的数据
我有一个 UIPicker 视图,我需要从 JSON feed 加载数据。 我能够将数组数据写入 NSLOG 并显示它,但无法在 UIPickerview 中看到它。请参阅下面的代码并帮助我,
还请让我知道如何在单击 UIbutton 时显示 UIPicker 视图,并在选择值时关闭 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);
}
i have a UIPicker view for which i need to load the data from a JSON feed.
I am able to write the array data into NSLOG and display it , but not able to see it in the UIPickerview . please see the code below and help me
Please also let me know how we can make the UIPicker view appear when i click a UIbutton and close the UIPickerview when a value is selected .
#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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题是这行代码:
这是将一个对象分配给您的“列表”变量,但该对象将被自动释放。相反,请尝试:
您可能应该使用实例变量而不是全局变量,尽管如果您一次只有一个网络连接,那么这并不重要。
I think the problem is this line of code:
This is assigning an object to your "list" variable, but that object will be autoreleased. Instead try:
And you should probably be using instance variables rather than global variables, although if you have only one network connection at a time it doesn't really matter much.