Re: who stole my UIView?
Re: who stole my UIView?
- Subject: Re: who stole my UIView?
- From: Luke the Hiesterman <email@hidden>
- Date: Thu, 12 Aug 2010 16:36:51 -0700
UIView does not maintain a tree. The view tree is really a CALayer tree, where some (or all) of the layers belong to UIView instances. This tie is made by the fact that UIView is the layer's delegate. Thus self.subviews is really self.layer.sublayers for each layer whose delegate is a UIView. Long story short, don't change the layer's delegate. If you want to act as a layer's delegate you should create a CALayer instead of a UIView and then do [self.view.layer addSublayer:layer];
Luke
On Aug 12, 2010, at 4:28 PM, Matt Neuburg wrote:
> Here's my code, simplified (and tested in this simple form) in order to
> demonstrate a mystery:
>
> - (void) illuminate: (NSArray*) arr {
> UIView* v = [[UIView alloc] initWithFrame:self.view.bounds];
> NSLog(@"%@", v);
> CALayer* lay = [v layer];
> v.tag = 111;
> [self.view addSubview:v];
> lay.delegate = self; // <-- ***
> [v release];
> [self performSelector:@selector(unilluminate:)
> withObject:nil afterDelay:0.2];
> return;
> }
>
> - (void) unilluminate: (id) dummy {
> for (UIView* aview in [self.view subviews])
> NSLog(@"%i %@", aview.tag, aview);
> }
>
> The mystery is what we learn in unilluminate from logging:
>
> (1) the UIView "v" is *not* among the subviews of self.view;
>
> (2) but it *is* among the subviews of self.view if we comment out the
> starred line (lay.delegate = self).
>
> How can this be? How can merely setting the layer's delegate make such a
> weird change? It appears that setting a layer's delegate somehow messes up
> the way the subview hierarchy works.
>
> Oh, I forgot to mention, even weirder: when lay.delegate = self and I
> implement drawing in the layer's context as the delegate, the drawing *does*
> appear in the right place in the window, even though the view whose drawing
> I can see right there in the window is not a subview of the thing I told it
> to be a subview of. - m.
>
> --
> matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/
> pantes anthropoi tou eidenai oregontai phusei
> Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pf
> AppleScript: the Definitive Guide, 2nd edition
> http://www.tidbits.com/matt/default.html#applescriptthings
> Take Control of Exploring & Customizing Snow Leopard
> http://tinyurl.com/kufyy8
> RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
> TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
>
>
>
> _______________________________________________
>
> 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
_______________________________________________
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