ios:异步请求后重新加载UIPickerView



我有一个picker视图,我希望从NSArray中填充它,我已经通过对JSONAPI的异步请求填充了它。我相信下载&picker函数同时发生,因此picker显示为空。我希望在connectionDidFinishedLoading方法上reloadAllComponents

我最初试图将选择器设置为出口,然后引用它,但它导致应用程序崩溃
我如何引用picker视图?!

#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

在用新数据更新"self.days"之后,您应该在UIPickerView实例上调用reloadComponent:reloadAllComponents:方法。

最新更新