Reputation: 6532
My app has an UITabViewController with 3 tabs. The first two tabs will read some data from disk and display it (done in viewDidLoad of the first two tabs).
The third tab has some kind of config information. If the user changes the config information in the third tab, i want the first two tabs to be refreshed, i.e., viewDidLoad should be re-called.
I cannot use viewWillAppear in the first two tabs, as the read from disk part is kind of intensive and I wouldn't want to do it everytime the tab is clicked. Also, I need to do some auxiliary tasks (in addition to updating the first two tabs) when the third tab data is edited, so I want to reload the tabs via viewDidLoad, while doing those auxiliary tasks.
Upvotes: 0
Views: 1277
Reputation: 1229
You can use the -(void)viewWillAppear:(BOOL)animated
method to trigger the refresh on the other two view controllers.
If you don't want to reload the data every time the user clicks the Tab you may use NSNotifications to trigger a refresh. See a detailed explanation at: http://www.numbergrinder.com/2008/12/patterns-in-objective-c-observer-pattern/
Upvotes: 1
Reputation: 11993
Use NSNotifications to do this.
Since the third tab is your config settings you will probability want to be storing these in NSUserDefaults
so use the NSUserDefaultsDidChangeNotification
to watch for this in your viewDidLoad
method and move your reloadData code into its own method.
- (void)viewDidLoad
{
[super viewDidLoad];
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self
selector:@selector(userDefaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
[self reloadData];
}
Now this will trigger a call to the method userDefaultsChanged:
whenever your defaults are changed, add the method as follows.
- (void)userDefaultsChanged:(NSNotification *)notification
{
[self reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnLoad];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
Edit: Alternative method to watch for specific default values
[[NSUserDefaults standardUserDefaults] addObserver:self
forKeyPath:@"SomeDefaultKey"
options:NSKeyValueObservingOptionNew
context:NULL];
- (void)observeValueForKeyPath:(NSString *) keyPath ofObject:(id) object change:(NSDictionary *) change context:(void *) context
{
if([keyPath isEqual:@"SomeDefaultKey"])
{
// Do Something
}
if([keyPath isEqual:@"SomeOtherKey"])
{
// Do Something else
}
}
Upvotes: 3
Reputation: 3475
I would use -(void)viewWillAppear:(BOOL)animated
. To get around the read from disk being 'kind of intensive' you could set a flag when the config changes in the 3rd tab and then only read from disk in the other tabs if that flag is set
Upvotes: 1