objective c - What's the regular pattern to implement `-init` chain when subclassing UIView? -


when make object use in nib, object should implement -initwithcoder:. can't figure out regular pattern implementing this.

i have been used code.

- (id) initwithcoder:(nscoder *)adecoder {  if(self=[super initwithcoder:adecoder])  {      // initialize object.  }  return self; } 

but have make same code uiview instantiated code this.

- (id) initwithcoder:(nscoder *)adecoder {  if(self=[super initwithcoder:adecoder])  {      // initialize object.  }  return self; } - (id) initwithframe:(cgrect)frame {  if(self=[super initwithframe:frame])  {      // initialie obejct.  }  return self; } 

i feels wrong. recommendations?

*of source initialization logic can extracted new method, it's not point.

use awakefromnib initialize uiview that's being created nib file. if want make general can created either nib file or programmatically, make method configureobject , call both designated initializer (often initwithframe:) , awakefromnib.

- (void)configureobject {     // initialization here }  - (id)initwithframe:(cgrect)frame {     if(self = [super initwithframe:frame])     {         [self configureobject];     }      return self; }   - (void)awakefromnib {     [self configureobject]; } 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -