I have a tabBarController containing three tabs. One tab is a settings tab, it has three UISwtch's which are contained in a UITableView, and it stores the data in [NSUserDefaults standardUserDefaults]. I'm testing everything a user could possibly do and have found a small problem. When the settings tab is in the foreground and I exit the app, enter the main settings for the phone, alter the settings for my app, exit settings then restart my app, the settings tab is still in the foreground but the toggle switches do not reflect the changes made in the previous step. If i switch to another tab then back to my settings tab the changes are now reflected.
I've tried everything, viewWillAppear, viewDidAppear, [theTtableView reloadData], nothing works. The strange thing is the exact same functions are called when it resumes and the settings tab is in the forground, as when another tab is in the foreground then I select the settings tab. I just can't get it to refresh, even though viewDidAppear and viewWillAppear are both being called on the settings controller.
I know is probably unlikely a user will be in the in app settings tab, then exit and go to the phone settings tab to make a change, then back to the app, but it's driving me nuts and I need to fix this.
I've tried every suggestion I could find here, still can't make it work.
Any ideas?
Thanks
Call this
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshViews) name:UIApplicationWillEnterForegroundNotification object:nil];
in your view controller's initialization (replace refreshViews
with selector of the method you refresh your switches to reflect the actual values in).
Don't forget to [[NSNotificationCenter defaultCenter] removeObserver:self]
in dealloc
.
Alternatively you can register for the notification in viewDidLoad
or viewWillAppear
and unregister in viewDidUnload
orviewWillDisapper
respectively.