I am using NSUSerDefaults to store a couple strings and integers for my application. Whenever a view is opened, the string is loaded slower than the view so you see a glitch. For example, I save the selectedSegmentIndex and then read it in viewDidAppear and for a quick moment when the view is called, no segment is selected, then the right one selects. How do you make it so there is no time gap between the view being opened and the setting be read?
- (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];
}