execute code in viewwillappear once

Tag: layer , viewwillappear Author: ocean_yliu Date: 2012-09-06

I have a few line of code in viewwillappear method. I want to execute this code only once. How can i do this. However this code is inside a loop.

CALayer *myLayer = btn.layer; myLayer.borderColor = [UIColor blackColor].CGColor; myLayer.masksToBounds=NO; myLayer.borderWidth = 2.0; myLayer.shadowOffset = CGSizeMake(0, 3); myLayer.shadowRadius = 10.0; myLayer.shadowColor = [UIColor blackColor].CGColor; myLayer.shadowOpacity = 1.0;

What do you mean? Is this a part of the loop, and you want to execute this part of the code only once, or do you want to make the loop run only once? Anyway, it would be helpful if you'd post the context.

Best Answer

Declare a BOOL variable flag (or any name you like) in .h file. set it to NO in viewDidLoad method. In viewWillAppear method, have a check if this code is executed before:

for(---)
{
if(!flag)
 {
      //execute above lines of code
   flag = YES;
 }
}

just in last line, make its value YES.

comments:

Thanks for your answer. It works well for me.
@Lotus. Please mark this answer as solution by clicking on check you are seeing here on left hand side. Thanks :)
good logic Engr Anum :) Thanks