ios:在async-request之后重新加载UIPickerView

时间:2014-10-23 11:57:06

标签: ios objective-c uipickerview

我有一个选择器视图,我希望从我通过异步请求填充的NSArray填充到JSON api。我相信下载&选择器功能同时发生,因此选择器显示为空。我希望reloadAllComponents方法connectionDidFinishedLoading

我最初尝试将选择器设置为插座,然后由此引用,但它导致应用程序崩溃 如何引用选择器视图?!

#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 个答案:

答案 0 :(得分:2)

你应该在`self.days'之后在你的UIPickerView实例上调用reloadComponent:reloadAllComponents:方法。已更新新数据。