I'm trying to implement state restoration in an app that uses iOS 6+ and storyboards, but I am having problems finding a way to prevent duplicate calls to heavy methods.
If I simply start the app, then I need to setup the UI in viewDidLoad
:
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
}
This works fine in a normal, non-state-restoration world. Now I've added state restoration and after restoring some properties I need to update the UI with those properties:
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder {
[super decodeRestorableStateWithCoder:coder];
// restore properties and stuff
// [...]
[self setupUI];
}
So what happens now is that first the setupUI
method is called from viewDidLoad
, and then again from decodeRestorableStateWithCoder:
. I don't see a method that I can override that's always called last.
This is the normal order of method calls:
- awakeFromNib
- viewDidLoad
- viewWillAppear
- viewDidAppear
When using state restoration, this is called:
- awakeFromNib
- viewDidLoad
- decodeRestorableStateWithCoder
- viewWillAppear
- viewDidAppear
I can't place the call to setupUI
in viewWillAppear
because then it would also be executed every time you native back to a view.
It would be much handier if decodeRestorableStateWithCoder
was called BEFORE viewDidLoad
because then you could use restored properties. Sadly that not the case, so... how can I prevent doing the work in viewDidLoad
when I know that I need to do it all over again in decodeRestorableStateWithCoder
right after?
NO
inviewDidLoad
because of defaultBOOL
value. – Padang