ios - UIViewController lifecycle calls in combination with state restoration -


i'm trying implement state restoration in app uses ios 6+ , storyboards, having problems finding way prevent duplicate calls heavy methods.

if start app, need setup ui in viewdidload:

- (void)viewdidload {     [super viewdidload];     [self setupui]; } 

this works fine in normal, non-state-restoration world. i've added state restoration , after restoring properties need update ui properties:

- (void)decoderestorablestatewithcoder:(nscoder *)coder {     [super decoderestorablestatewithcoder:coder];     // restore properties , stuff     // [...]     [self setupui]; } 

so happens first setupui method called viewdidload, , again decoderestorablestatewithcoder:. don't see method can override that's called last.

this normal order of method calls:

  • awakefromnib
  • viewdidload
  • viewwillappear
  • viewdidappear

when using state restoration, called:

  • awakefromnib
  • viewdidload
  • decoderestorablestatewithcoder
  • viewwillappear
  • viewdidappear

i can't place call setupui in viewwillappear because executed every time native view.

it handier if decoderestorablestatewithcoder called before viewdidload because use restored properties. sadly not case, so... how can prevent doing work in viewdidload when know need on again in decoderestorablestatewithcoder right after?

if you're doing state restoration programatically (i.e. not using storyboards), can use + viewcontrollerwithrestorationidentifierpath:coder:, init view controller there , use whatever need coder pre-viewdidload initialization.

+ (uiviewcontroller *)viewcontrollerwithrestorationidentifierpath:(nsarray *)identifiercomponents coder:(nscoder *)coder {     if ([[identifiercomponents lastobject] isequaltostring:kviewcontrollerrestorationidentifier]) {         if ([coder containsvalueforkey:kidtorestore]) {             // can restore if have id, otherwise return nil.             int savedid = [coder decodeintegerforkey:kidtorestore];             viewcontroller *vc = [[viewcontroller alloc] init];             [vc setthingid:savedid];             return vc;         }     }      return nil; } 

i've found trying implement state restoration has shown bad programming practices in code, packing viewdidload. while works (if you're not using storyboards), other option refactor how you're setting view controllers. instead of using flag, move code pieces own methods , call methods both places.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -