目前正致力于使用CoreData查找列车的应用程序。基本上,每列火车都存有相应的一组目击物。当用户看到火车时,他们会记录目击事件并将目击事件与火车序列号联系起来(只是基本数据关系)
我尝试使用序列号填充UIPickerView
但是我遇到了一些困难。我计划多次使用这个特定的PickerView,所以它有自己的类,并没有在ViewController中实现。
我已正确设置了委托和dataSource,但从未填充PickerView。从每个函数中的NSLog
和printf
代码我可以看出titleForRow
永远不会被调用,numberOfRowsInComponent
也不是UIPickerView
这是我{{1}的代码} 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代码:
- (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.
}
答案 0 :(得分:1)
TrainPicker需要是视图控制器的实例变量,以便它不被垃圾收集(在.h中作为属性或在.m文件的接口部分中)。在viewDidLoad中为它创建局部变量将导致在使用ARC时对其进行垃圾回收。