在启动时加载上次选择的UIPickerView

时间:2012-02-18 16:52:58

标签: iphone objective-c ios5 xcode4.2 uipickerview

我有一个UIPickerView来选择应该显示的PDF。我想保存最后选择的组件,因此当我退出应用程序或重新启动设备时,它将自动显示最后选择的PDF。我有两个组件,所以我想知道它是如何完成的......谢谢!

2 个答案:

答案 0 :(得分:0)

您可以使用NSUserDefaults将所选行保存在selectedRowInComponent中... 然后在展示picker时使用selectRow:inComponent:animated:

修改

保存这样。

int selectedRow = [picker selectedRowInComponent:componentNumber];

答案 1 :(得分:0)

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

    NSString *numberString = [array1 objectAtIndex:[_pickerView selectedRowInComponent:0]];
    NSString *letterString = [array2 objectAtIndex:[_pickerView selectedRowInComponent:1]];

    NSString *pathComponentString = [NSString stringWithFormat:@"Stundenplan_%@%@.pdf", numberString, letterString];
    stundenplan.scalesPageToFit = YES;
    [stundenplan loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:pathComponentString]]]];
    NSInteger selectedRow = [_pickerView selectedRowInComponent:component];
    NSString *key = [NSString stringWithFormat:@"%@", @"picker"];
    [[NSUserDefaults standardUserDefaults] setInteger:selectedRow forKey:key];

}

-(IBAction)showPicker:(id)sender
{

    [pickerView selectRow:[[[NSUserDefaults standardUserDefaults] objectForKey:@"picker"] intValue] inComponent:0 animated:NO];
    self.pickerView.hidden = NO;
    cancelbtn.enabled = NO;
    cancelbar.hidden = NO;

}