-1

In My Application i need to release my NSTimer , when i am moving from one view controller to another view controller . How to release this type of objects in ARC ? i am using below code for creation and releasing NSTimer but Where i have to write this releasing code in view controller?

For Creation.

- (void)viewDidLoad{
    [super viewDidLoad];
    updateBCK = [NSTimer scheduledTimerWithTimeInterval:(5.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    [updateBCK fire];
}


-(void)changeImage{

            static int i=0;
            if (i == [myImages count]){
                i=0;
            }
            [UIImageView beginAnimations:nil context:NULL];
            [UIImageView setAnimationDuration:0.6];
            mainBackgroundImageView.alpha=1;
            mainBackgroundImageView.image =[myImages objectAtIndex:i];
            NSLog(@"\n The main screen image is %@",[myImages objectAtIndex:i]);
            [UIImageView commitAnimations];
            i++;
        }

For Release.

[updateBCK invalidate];//
    updateBCK = nil;

Thanks in Advance.

ssmanohar
  • 186
  • 1
  • 9
  • where is your code which pushes the new view controller – vishnuvarthan Aug 25 '14 at 12:14
  • I am using Timer to displaying background images randomly in a First View controller. – ssmanohar Aug 25 '14 at 12:26
  • You said you want to turn timer off when you switch to another view controller isn't that right ???.if so invalidate your timer in that switching method.i can give you exact answer if you post that part of the code – vishnuvarthan Aug 25 '14 at 12:32
  • i am using array of images to display randomly in my first view controller and also crate on button in first view controller , when i am pressing button , i need to go to second view controller and also i have to stop timer in first view controller. – ssmanohar Aug 25 '14 at 12:40

5 Answers5

1

You should call

[timer invalidate];
timer = nil;

where you push your view controller. If this is an issue, you can still call it in

- (void) viewWillDisappear:(BOOL)animated;

Also, you should initialize it in

- (void) viewDidAppear:(BOOL)animated;

That makes more sense.

delannoyk
  • 1,216
  • 1
  • 12
  • 22
  • yes, that makes me more sense. Finally solved my problem with below code. -(void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; updateBCK = [NSTimer scheduledTimerWithTimeInterval:(3.0) target:self selector:@selector(changeImage) userInfo:nil repeats:YES]; [updateBCK fire]; } -(void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated]; if ([updateBCK isValid]){ [updateBCK invalidate]; updateBCK = nil; } } – ssmanohar Aug 27 '14 at 11:22
  • Nice work. Consider accepting this as an answer :) ! – delannoyk Aug 27 '14 at 11:48
0

When you want to stop time

use below code

[timer invalidate];  // timer is the name of timer object
Muhammad Adnan
  • 2,668
  • 15
  • 27
0
- (IBAction)button:(id)sender {

SecondViewController *second = [[SecondViewController alloc] 
       initWithNibName:@"secondViewController" 
       bundle:nil];


    [self presentViewController:second animated:NO completion:nil];

[self.timer invalidate];  // timer is the name of timer object
timer=nil;//it may work without this line too ;not sure

}

vishnuvarthan
  • 492
  • 1
  • 6
  • 23
  • this logic is not working ,if when you are taking 3 view controllers. this logic worked in between 2nd and 3rd View controllers . but not work in 1st and 2nd view controllers. – ssmanohar Aug 25 '14 at 13:04
  • This Should work between two view controllers Irrelevant of its position.If it is working between two and three it should work between one and two also. – vishnuvarthan Aug 25 '14 at 13:14
  • Are you able two switch from view controller one to two using this code. – vishnuvarthan Aug 25 '14 at 13:15
0

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Timers/Articles/usingTimers.html

A timer maintains a strong reference to its target. This means that as long as a timer remains valid, its target will not be deallocated. As a corollary, this means that it does not make sense for a timer’s target to try to invalidate the timer in its dealloc method—the dealloc method will not be invoked as long as the timer is valid.

-1

when i am moving from one view controller to another view controller

Then you should do this in delloc, if you want to do some cleanup tasks when your view is dismissing or released. It's the best place, In such case you can implement it.

-(void)dealloc{

[updateBCK invalidate];//
    updateBCK = nil;

}

Hope this helps

Sarim Sidd
  • 2,166
  • 2
  • 22
  • 31