Search code examples
layerviewwillappear

execute code in viewwillappear once


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;


Solution

  • 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.