Currently working on an app for spotting trains using CoreData. Basically, each train is stored with a corresponding set of sightings. When a user sees a train they log a sighting and that sighting is tied back to a train serial number (just basic data relationships)
I'm trying to populate a UIPickerView
with serial numbers however I am running into some difficulty. I'm planning on using this specific PickerView multiple times, so it has its own class and is not implemented in the ViewController.
I have set the delegate and dataSource correctly, but the PickerView is never populated. From the NSLog
and printf
code that is in each function I can tell that titleForRow
is never called, and neither is numberOfRowsInComponent
Here is my code for the UIPickerView
class:
-(id)init
{
//init super class.
self = [super init];
//get allocate array reader (TrainSightingReaderWriter) then set the array that this picker will be getting data from.
[self setArrayReader:[[TrainSightingReaderWriter alloc] init]];
[self setArray: [[self arrayReader] getTrainSerialArray]];
NSLog(@"INIT PickerView");
//return this object.
return self;
}
-(int)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
printf("At component counter\n");
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
printf("At counter. %d is output\n", _array.count);
return _array.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"Returning a value.");
return [self array][row];
}
ViewController code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"We have made it to the AddSightVC.");
TrainPicker * mypicker;
mypicker = [[TrainPicker alloc]init];
//ISSUES
_trainPickerScroller.delegate = mypicker;
_trainPickerScroller.dataSource = mypicker;
[_trainPickerScroller reloadAllComponents];
[_trainPickerScroller reloadInputViews];
// Do any additional setup after loading the view.
}
TrainPicker needs to be an instance variable for the view controller so that it is not garbage collected (either in the .h as a property or in an interface section in the .m file). Creating a local variable in viewDidLoad for it will cause it to be garbage collected if you are using ARC.