我正在使用 NSUSerDefaults 为我的应用程序存储几个字符串和整数。每当打开视图时,字符串的加载速度都会比视图慢,因此您会看到故障。例如,我保存 selectedSegmentIndex,然后在 viewDidAppear 中读取它,当视图被调用时,没有选择任何段,然后选择正确的段。您如何做到这一点,以便在打开视图和读取设置之间没有时间间隔?
- (void)viewDidLoad
{
[super viewDidLoad];
int segmentIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedIndex"];
unitSegmentControl.selectedSegmentIndex = segmentIndex;
BOOL location = [[NSUserDefaults standardUserDefaults] boolForKey:@"locationManager"];
[gpsSwitch setOn:location animated:NO];
deviceID.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"DeviceID"];
}
- (IBAction)changeSeg:(id)sender {
if (unitSegmentControl.selectedSegmentIndex == 0) {
[[NSUserDefaults standardUserDefaults] setObject:@"http://98.246.50.81/firecom/xml/units/E01.xml" forKey:@"parserURL"];
[[NSUserDefaults standardUserDefaults] setObject:@"Hillsboro Main" forKey:@"selectedStation"];
[[NSUserDefaults standardUserDefaults] setObject:@"Hillsboro Fire & Rescue" forKey:@"selectedDepartment"];
}
if (unitSegmentControl.selectedSegmentIndex == 1) {
[[NSUserDefaults standardUserDefaults] setObject:@"http://98.246.50.81/firecom/xml/units/E02.xml" forKey:@"parserURL"];
[[NSUserDefaults standardUserDefaults] setObject:@"Hillsboro Witch Hazel" forKey:@"selectedStation"];
[[NSUserDefaults standardUserDefaults] setObject:@"Hillsboro Fire & Rescue" forKey:@"selectedDepartment"];
}
[[NSUserDefaults standardUserDefaults] setInteger:unitSegmentControl.selectedSegmentIndex forKey:@"selectedIndex"];
[[NSUserDefaults standardUserDefaults] synchronize];
}