I got a model object that contains movies and actors arrays. I'm trying to populate 2 pickers (1 picker with movies and the other with actors) in the same ViewController. Please help as I'm still learning how it works.
Thanks!
My code is below...
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.model = [[MovieModel alloc] init];
for(int i = 0; i < self.model.count; i++){
NSString *movies = [self.model movieForIndex:i];
NSLog(@" Movie = %@", movies);
}
for(int i = 0; i < self.model.count; i++){
NSString *actors = [self.model actorForIndex:i];
NSLog(@" Actor = %@", actors);
}
}
//This is the place where I'm having issues. Thanks!!!
#pragma mark - Picker View Delegate
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0){
return [self.model movieForIndex: row];
}
else if (component == 1){
return [self.model actorForIndex: row];
}
else
return nil;
}
#pragma mark - Picker View Data Source
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.model.count;
}
@end
First you need to make sure your ViewController implements UIPickerViewDataSource and UIPickerViewDelegate.
You need to add the picker to your view:
Your picker has two components - 1 for movies and 1 for actors:
Then set the number of rows for each component: