Forcing Reload of View when returning from FlipSideView of Utility Application
I've got a Utility Application for iPhone 3.1.3. I have my main view loading fine. I flip over to the flipside view of the utility app an开发者_运维知识库d change some settings. How can I call the code I've currently got in viewDidLoad
in the main view to refresh it, as settings have changed?
Do I create a public method and call it from the viewDidUnload method of the flipside view? Do I call viewDidLoad
directly? What's the best way to do this?
Firstly, you should never call viewDidLoad
directly. It probably won't cause any problems, but it's best to leave a method like that to be called by the underlying API only. Just move out some of the view initialization into a new method like configureViewForSettings
or something along those lines.
The best approach would be to use delegation. This is most suitable when you need to send back some sort of data or notification to a 'parent' view. Create a new protocol MySettingsViewDelegate
with a method like settingsViewChangedSettings:(MySettingsView *)settingsView
. Then on your settings view controller add a id<MySettingsViewDelegate> delegate
property.
When the settings change call the delegate method like this:
[self.delegate settingsViewChangedSettings:self];
And in your main view controller, when you create/display the settings view, set yourself to be the delegate
mySettingsView.delegate = self;
/* Display the settings view */
Then somewhere:
- (void)settingsViewChangedSettings:(MySettingsView *)settingsView {
/* Settings changed, refresh your view */
[self configureViewForSettings];
}
精彩评论