2

I would like to know if there is a method equivalent to awakeFromNib.

My app has 2 views and the 2nd view has a subclass (UIView) which I use to draw.

I use a timer which is called from awakeFromNib in order to animate the drawing.

- (void) awakeFromNib
{
    timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:selfselector:@selector(ReDraw) userInfo:nil repeats:YES];
}

- (void) ReDraw
{
    i++;
    [self setNeedsDisplay];
}

Now when i go back to my first view and come back to this view the drawing is still on (awakeFromNib is not called again).

The timer doesn't stop when i go back to the first view. I want the timer to stop when i go back and the drawing to restart when i visit the second view again.

The methods viewdidLoad and initWithFrame: do not work inside the subclass.

pkamb
  • 33,281
  • 23
  • 160
  • 191
R3D3vil
  • 681
  • 1
  • 9
  • 22

1 Answers1

1

awakeFromNib, viewdidLoad and initWithFrame are all called during the initialization phase of the object. If the object is already created, those methods will not be called again. You can force a view to draw itself by using the setNeedsDisplay method of the view.

David
  • 14,205
  • 20
  • 97
  • 144
  • So how do i stop the timer when i go back to my first view and fire it from scratch again when i go to the view again? – R3D3vil Apr 06 '11 at 20:07
  • You you create your timer, retain it as in instance variable in a controller class (UIViewController perhaps). When you switch a the view invalidate your timer and then start it again if you have to. – David Apr 06 '11 at 21:15
  • ok... but i cannot start it again since i use awakeFromNib to use the timer first time. I cannot even call a method inside the class(with drawRect method) which is a subclass of UIview from another class. – R3D3vil Apr 08 '11 at 16:06