UIPickerView没有加载行

时间:2016-08-25 10:45:17

标签: ios objective-c core-data uipickerview

目前正致力于使用CoreData查找列车的应用程序。基本上,每列火车都存有相应的一组目击物。当用户看到火车时,他们会记录目击事件并将目击事件与火车序列号联系起来(只是基本数据关系)

我尝试使用序列号填充UIPickerView但是我遇到了一些困难。我计划多次使用这个特定的PickerView,所以它有自己的类,并没有在ViewController中实现。

我已正确设置了委托和dataSource,但从未填充PickerView。从每个函数中的NSLogprintf代码我可以看出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.
}

1 个答案:

答案 0 :(得分:1)

TrainPicker需要是视图控制器的实例变量,以便它不被垃圾收集(在.h中作为属性或在.m文件的接口部分中)。在viewDidLoad中为它创建局部变量将导致在使用ARC时对其进行垃圾回收。