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