[self setLayer:] in initWithFrame:

  • I provide an initWithFrame: for my NSView subclass,
    and as it is a stand-in for a Custom View in IB the
    method is called. It does initialize all my instance
    variables as expected.

    However, when I try to turn my view into a
    layer-hosting view with:
    CALayer* rootLayer = [CALayer layer];
    [self setLayer:rootLayer];
    [self setWantsLayer:YES];
    [rootLayer setDelegate:self];
    [rootLayer setNeedsDisplay];

    My delegate method is never called, nor are any
    borders shown if I set those properties on my
    rootLayer. (The delegate does get a look, though,
    since I can crash the app by feeding a garbage pointer
    as a delegate.) Moving this code into my view's
    awakeFromNib handler makes the delegate work as
    expected.

    Why can't I enable layer hosting in my initWithFrame:
    method?

    thanks,
    -natevw

          ____________________________________________________________________________________
    ¡Capacidad ilimitada de almacenamiento en tu correo!
    No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
    http://correo.espanol.yahoo.com/
  • Hi Nathan,

    If you move your layer code into awakeFromNib you should have much
    better results.

    If you don't have layer backing turned on in the nib file the view
    loading machinery turns off layer backing after initWithFrame: is
    called.

    HTH,

    -bd-
    http://bill.dudney.net/roller/objc

    On Feb 21, 2008, at 7:12 PM, Nathan Vander Wilt wrote:

    > I provide an initWithFrame: for my NSView subclass,
    > and as it is a stand-in for a Custom View in IB the
    > method is called. It does initialize all my instance
    > variables as expected.
    >
    > However, when I try to turn my view into a
    > layer-hosting view with:
    > CALayer* rootLayer = [CALayer layer];
    > [self setLayer:rootLayer];
    > [self setWantsLayer:YES];
    > [rootLayer setDelegate:self];
    > [rootLayer setNeedsDisplay];
    >
    > My delegate method is never called, nor are any
    > borders shown if I set those properties on my
    > rootLayer. (The delegate does get a look, though,
    > since I can crash the app by feeding a garbage pointer
    > as a delegate.) Moving this code into my view's
    > awakeFromNib handler makes the delegate work as
    > expected.
    >
    > Why can't I enable layer hosting in my initWithFrame:
    > method?
    >
    >
    > thanks,
    > -natevw
  • > If you don't have layer backing turned on in the nib
    > file the view
    > loading machinery turns off layer backing after
    > initWithFrame: is
    > called.

    Ah, that explains it!

    > From my understanding (and the docs don't elaborate on
    this much at all), I *don't* want to do that, since I
    need to provide my own layer before  calling
    wantsLayer:, correct?

    thanks,
    -natevw

          ____________________________________________________________________________________
    ¡Capacidad ilimitada de almacenamiento en tu correo!
    No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
    http://correo.espanol.yahoo.com/
  • Hi Nate,

    Glad it works!

    You are correct that when you want to set up you own layer hierarchy
    its best to leave that for awakeFromNib instead of setting the check
    box. When you check that a view wants a layer in IB and then replace
    it its just wasting creating that layer and such. Also I have
    occasionally seen some strange behavior from views that had that
    checkbox turned on in IB after I replace it's layer. I think that when
    a view comes out of a nib thinking that its layer backed (instead of
    layer hosting) it makes some assumptions about what it can do with/to
    the layer that are not valid once you replace the layer.

    So I have had better luck just leaving that checkbox unchecked in IB.

    anyway sorry to ramble, good luck!

    -bd-
    http://bill.dudney.net/roller/objc

    On Feb 21, 2008, at 9:11 PM, Nathan Vander Wilt wrote:

    >> If you don't have layer backing turned on in the nib
    >> file the view
    >> loading machinery turns off layer backing after
    >> initWithFrame: is
    >> called.
    >
    > Ah, that explains it!
    >
    > From my understanding (and the docs don't elaborate on
    > this much at all), I *don't* want to do that, since I
    > need to provide my own layer before  calling
    > wantsLayer:, correct?
    >
    > thanks,
    > -natevw
    >
    >
    >
    > ____________________________________________________________________________________
    > ¡Capacidad ilimitada de almacenamiento en tu correo!
    > No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
    > http://correo.espanol.yahoo.com/
previous month february 2008 next month
MTWTFSS
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29    
Go to today