2014-10-23 4 views
1

NSArray에서 JSON API에 대한 비동기 요청을 통해 채워 넣은 피커 뷰가 있습니다. 다운로드 & 선택 기능이 동시에 발생하므로 선택 도구가 비어있는 것으로 표시됩니다. 나는 connectionDidFinishedLoading 방법에 reloadAllComponents하고자합니다.

처음에는 선택기를 콘센트로 설정하려고 시도했지만 그다음 참고로 인해 앱이 중단되었습니다.
어떻게 피커 뷰를 참조합니까?!ios : async-request 후 UIPickerView를 다시로드하십시오.

#import "ViewController.h" 

@interface ViewController() 
@property NSArray *days; 
@end 

@implementation ViewController 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    return [self.days count]; 
} 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ 
    return self.days[row]; 
} 

// 
//Connection Methods 
// 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    // A response has been received, this is where we initialize the instance var you created 
    // so that we can append data to it in the didReceiveData method 
    // Furthermore, this method is called each time there is a redirect so reinitializing it 
    // also serves to clear it 
    _responseData = [[NSMutableData alloc] init]; 
    NSLog(@"did Receive Response..."); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    // Append the new data to the instance variable you declared 
    [_responseData appendData: data]; 
    NSLog(@"did Receieve Data..."); 

} 

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection 
        willCacheResponse:(NSCachedURLResponse*)cachedResponse { 
    // Return nil to indicate not necessary to store a cached response for this connection 
    return nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // The request is complete and data has been received 
    // You can parse the stuff in your instance variable now 
    self.days = [NSJSONSerialization JSONObjectWithData: _responseData options:NSJSONReadingMutableLeaves error:nil]; 
    NSLog(@"Finished Loading..."); 

    ///////////////////////// 
    //here i wish to reload// 
    ///////////////////////// 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    // The request has failed for some reason! 
    // Check the error var 
    NSLog(@"Fail With Error %@", error); 
} 


- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    // Create the request. 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.Example.co.uk/api.php"]]; 

    // Create url connection and fire request 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    NSLog(@"Request sent..."); 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

답변

1

당신은`self.days '후 당신의 UIPickerView 인스턴스에 reloadComponent: 또는 reloadAllComponents: 메소드를 호출해야하는 새로운 데이터로 업데이트되었습니다.

+0

부분적으로는 내 자신의 실수로, 필자의 초기 콘센트 @ pickerview에 문제가 있다고 생각합니다. reloadAllComponents로 다시 시도해 보았습니다! :) –

관련 문제