views:

47

answers:

2

I have the following code and I can't seem to get it to work.

First I create a view with a layer.

UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = view.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor whiteColor] CGColor], nil];
[view.layer insertSublayer:gradient atIndex:0];

Next I need to replace that layer. I have tried the following and it fails because I don't know how to access that layer 0. At this point I am in a different part of the program so I cannot just call gradient. I need to extricate it from view somehow.

[view.layer replaceSublayer:0 with:newgradient];

Apparently the 0 is supposed to be Old layer but I don't know how to access it.

Thanks.

+1  A: 

Keep a pointer to the gradient layer. If you have alot of them do it with an array. If you still don't want to do that you can always use this to get an array of all the layers..

[self.layer sublayers];

Then you can cycle through and check some parameter to see if they are the same. Like set the layer.name to something when you create it.

Justin Meiners
+1  A: 

I think you should be able to use this:

[view.layer replaceSublayer:[[view.layer sublayers] objectAtIndex:0]
            with:newGradient];

I'm away from my Xcode development environment and can't test this at the moment though.

GregInYEG
Perfect! Life is good!
John Smith