I have a tab bar app. One of the controllers is a “preference page”. What I am trying to do accomplish is access the uiswitch ivar that is on the “preference page” controller from the app delegate’s applicationWillTerminate method, however I am only getting the default IUSwitch value.
Here is the code:
- (void)applicationWillTerminate:(UIApplication *)application {
SettingsController *settings = [[SettingsController alloc] initWithNibName:@"SettingsView" bundle:nil]];
NSLog(@"settings preference value: %d", [settings isOn]);
}
This wont work because your are essentially creating another instance new of SettingsController which of course will not include a reference to the settings ivar in the original SettingsController. What you need to do is save a reference to the original SettingdController somewhere, either in the app delegate or in a singleton object.
Here’s a good blog on wether to use the app delegate or singleton method to pass around global references. I would personally use the singleton method.