UIPickerView无法从viewDidAppear加载

时间:2014-04-18 05:50:34

标签: ios objective-c uipickerview viewdidappear

我有一个UIPickerview,我最初是从viewDidLoad设置的。它工作得非常好。

但我希望每当我从另一个视图回来时刷新它,所以我放入viewDidAppear

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];


    NSLog(@"View will appear");
    [self loadthepicker];

}


-(void) loadthepicker
{

    pickerLoaderArray=[[NSMutableArray alloc] init];
    [pickerLoaderArray addObject:@"one"];
    [pickerLoaderArray addObject:@"two"];
    [pickerLoaderArray addObject:@"thee"];
    [pickerLoaderArray addObject:@"four"];

    [pickerView selectRow:0 inComponent:0 animated:NO];

}



-(NSInteger)numberOfComponentsInPickerView:(NSInteger)component
{

    return 1;

}


-(NSInteger)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(NSInteger)component
{

    return [pickerLoaderArray count];

}



-(NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    return [pickerLoaderArray objectAtIndex:row];
}

好的,我想我可能得到了答案:

[pickerView reloadAllComponents];

我添加了这个来加载thepicker函数,它现在工作正常。

这是正确的方法吗?

2 个答案:

答案 0 :(得分:1)

你必须致电

[pickerView reloadAllComponents];

答案 1 :(得分:0)

UIPickerview委托在viewDidApear方法之前调用的方法,因此你必须调用uipikerview explicit的delegates方法。

-(void) loadthepicker
{

pickerLoaderArray=[[NSMutableArray alloc] init];
[pickerLoaderArray addObject:@"one"];
[pickerLoaderArray addObject:@"two"];
[pickerLoaderArray addObject:@"thee"];
[pickerLoaderArray addObject:@"four"];

[self.pickerview selectRow:0 inComponent:0 animated:NO];
[self.pickerview reloadAllComponents];
}