我正在创建一个应用程序,在第一个视图中,用户可以选择登录或注册。在注册视图中是 a UITableViewCell
,当单击它时,会将用户带到包含 aUITableView
和 a的视图UIPickerView
。UITableView
工作正常,但是UIPickerView
应该动态提取它应该使用网络调用显示的数据的 正在显示,但看起来完全空白。加上几句NSLog
话,我注意到模型中使用拉取数据的方法AFNetworking
永远不会被调用。我已经为UIPickerViewDelegate
andUIPickerViewDataSource
方法发布了下面的代码,以及应该在模型中提取数据的方法。提前致谢。
UIPickerViewDelegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [[self.brain classChoicesForSignUp] objectAtIndex:row];
}
UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
size_t numberOfRows = [self.brain classChoicesForSignUp].count;
NSLog(@"Number of Rows: %@", [[NSNumber numberWithFloat:numberOfRows] stringValue]);
return numberOfRows;
}
注册PickerBrain.m
#import "SignUpPickerBrain.h"
#import "AFJSONRequestOperation.h"
@implementation SignUpPickerBrain
#pragma mark - Picker Data
- (NSArray *)classChoicesForSignUp {
NSLog(@"Class choices method called");
// Note that in my code, the actual URL is present here.
NSURL *url = [NSURL URLWithString:@"the URL"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success!");
NSLog([JSON description]);
} failure:nil];
[operation start];
[operation waitUntilFinished];
NSLog([operation responseJSON]);
return [operation responseJSON];
}
@end