0

I'm trying to copy a UIView with some properties. The UIView is overriding the drawLayer: method to draw custom shapes.

I'm using the NSKeyedArchiver, a pasteboard and NSKeyedUnarchiver to get back the properties. An instance of UIView is created and the properties are set - everything is fine till now.

Some properties get created in the drawlayer method, which have the default values, not the copied ones. My question is when initWithCoder is called, and an instance is created, isn't drawRect or drawLayer called? Why?

Prathiba
  • 287
  • 2
  • 4
  • 15

1 Answers1

1

drawLayer/drawRect are called on demand (sparingly) when content needs to be rendered or re-rendered. It's probably not called during initialization because there's no need. It should be called later when the view goes on screen and needs to be rendered.

Also, you say you are creating some properties in drawLayer? Consider moving those out to some initialization method and keep the drawing methods focussed on drawing only.

Nik
  • 5,801
  • 2
  • 31
  • 23
  • Hmm... ok.. what i am doing is drawing a shape and adding textviews to the shape after drawing... the textview properties are the properties i was talking about... how do u suggest i move this out? Because i cant add the textviews first(during initialization) and then draw... – Prathiba Jun 29 '12 at 07:18
  • Why can't you add the textviews first (during initialization)? If you know how to draw the shapes in drawRect, you can do the same computations in initialisation (given you know the view bounds) to figure out where to put the textview. So just add the textview in initialization then, and do the drawing later. Since the textview is a subview of your view, it should appear above any drawing you do in your view. – Nik Jun 29 '12 at 07:36
  • Worked! Maybe i need a break,coz my common sense has gone to dogs! Thanks though... – Prathiba Jun 29 '12 at 08:09