• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: initWIthFrame or awakeFromNib
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: initWIthFrame or awakeFromNib


  • Subject: Re: initWIthFrame or awakeFromNib
  • From: Joar Wingfors <email@hidden>
  • Date: Wed, 13 May 2009 21:32:33 -0700

...to follow up on that a bit: When you're instantiated from nib loading you will not see "-initWithFrame:" being called, but rather "- initWithCoder:". You can read more about that in the NSCoding & Nib Loading documentation.

You can in a case like this implement both "-initWithFrame:" and "- initWithCoder:" and then call the method that provides your shared initialization code from both of them, like Luke suggested.

I would add that if there's any chance that your view might in turn be subclassed, you should probably pick a unique name for the method, so that it's not stomped on by a subclass doing the same thing. Something like: "-commonInitForFoo", if Foo is the name of the class in question.

Finally, you need to treat "-initWithCoder:" just like you would any other init method. Unlike "-awakeFromNib" you must not attempt to access to any outlets, actions or bindings set up in the nib file - They probably haven't been re-established yet, as the nib file is in the process of being unarchived. If you need to reference anything configured in the nib file, you have to wait for "-awakeFromNib". That said, you can use "-initWithCoder:" for stuff that you would otherwise do in "-initWithFrame:" or any other initializer, in other words: Setting up your instance, without making any assumptions about how it's currently hooked up to the outside world.

j o a r


On 13 maj 2009, at 16.59, Luke the Hiesterman wrote:

If you have some initialization that needs to be done from multiple init points, it's common to create a method for this, say commonSetup. Then in all init paths, you would do [self commonSetup].

Luke

On May 13, 2009, at 4:56 PM, Ramakrishna Vavilala wrote:

I have a custome view class which I use to host some CALayers. As I specify
the view in the Nib file, I use awakeFromNib to create the CALayers. Part of
me however thinks that initWithFrame might be more suitable for this
initialization as it can be used for views created outside of interface
builder (not likely for my project).
Which method is the correct one?


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: initWIthFrame or awakeFromNib
      • From: Jonathan Hess <email@hidden>
References: 
 >initWIthFrame or awakeFromNib (From: Ramakrishna Vavilala <email@hidden>)
 >Re: initWIthFrame or awakeFromNib (From: Luke the Hiesterman <email@hidden>)

  • Prev by Date: Re: Special requirements for the window param of NSAlert beginSheetModalForWindow?
  • Next by Date: Re: Core Data Problem: Creating a child object creates unwanted parent
  • Previous by thread: Re: initWIthFrame or awakeFromNib
  • Next by thread: Re: initWIthFrame or awakeFromNib
  • Index(es):
    • Date
    • Thread