2011-09-18 111 views
0

我有一個UIPicker視圖,爲此我需要從JSON源加載數據。 我能夠將數組數據寫入NSLOG並顯示它,但無法在UIPickerview中看到它。請參閱下面的代碼,並幫助我UIPicker視圖不顯示來自JSON數組的數據

請讓我知道我們如何使UIPicker視圖出現,當我點擊一個UIbutton並關閉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); 
} 

回答

0

我認爲這個問題是這行代碼:

list = [dictionary valueForKey:@"products"]; 

這被分配一個對象到你的「名單」的變量,但對象將被自動釋放。相反,嘗試:

list = [[dictionary valueForKey:@"products"] retain]; 

而且你也許應該使用實例變量,而不是全局變量,但如果你有在同一時間只有一個網絡連接它並不真正的問題太多。

+0

我試過了。但似乎沒有工作。 – UnlimitedMeals

+0

當我嘗試更改行和打印列表objectAtIndex:1我能夠NSLog的值。奇怪,但它並沒有顯示在pickerView上 – UnlimitedMeals

+0

我確實調整了代碼並嘗試了一些差異化的東西,但仍然有力。我已更新智慧我最新的代碼。任何人都可以幫忙 – UnlimitedMeals

相關問題