user1452248
user1452248

Reputation: 767

invalidate and fire Timer from different view controller is not working

myTimer is defined in OneViewController and i m trying to invalidate and fire it again from mainviewcontroller but it is not working. What exactly i m missing here.

here is my code

OneViewController.h

@property (nonatomic, strong) NSTimer *myTimer;

OneViewController.m

@synthesize myTimer;

- (void)viewDidLoad
{

 [super viewDidLoad];


[self myTimerMethod];}

- (void)myTimerMethod{

 NSLog(@"myTimerMethod is Called");

self.myTimer = [NSTimer scheduledTimerWithTimeInterval:2.4
                                           target:self
                                         selector:@selector(updateView:)
                                         userInfo:nil
                                          repeats:YES];

 }


 - (void)updateView:(NSTimer *)theTimer
  {
if  (index < [textArray count])
 {
   self.textView.text = [self.textArray objectAtIndex:index];
   self.imageView.image = [self.imagesArray objectAtIndex:index];
   index++;
}else{

    index = 0;


   }
  }


  MainViewController.h

 @class OneViewController;

 @property (strong, nonatomic) OneViewController *oneviewcontroller;


 MainViewController.m

  @synthesize oneviewcontroller = _oneviewcontroller;

-(void)playpauseAction:(id)sender {

if([audioPlayer isPlaying])
{
    [sender setImage:[UIImage imageNamed:@"music.png"] forState:UIControlStateNormal];

    [audioPlayer pause];

    [self.oneviewcontroller.myTimer invalidate];

}else{

    [sender setImage:[UIImage imageNamed:@"audiostop.png"] forState:UIControlStateNormal];

    [audioPlayer play];


    [self.oneviewcontroller.myTimer fire];



    if(isFirstTime == YES)
    {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:06.0
                                                      target:self
                                                    selector:@selector(displayviewsAction:)
                                                    userInfo:nil
                                                     repeats:NO];
        isFirstTime  = NO; } } }


  - (void)displayviewsAction:(id)sender{

OneViewController *oneviewcontroller = [[OneViewController alloc] init];

oneviewcontroller.view.frame = CGRectMake(0, 0, 320, 430);

CATransition *transitionAnimation = [CATransition animation];

[transitionAnimation setDuration:1];

[transitionAnimation setType:kCATransitionFade];

[transitionAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];

[self.view.layer addAnimation:transitionAnimation forKey:kCATransitionFade];

[self.view addSubview:oneviewcontroller.view];

 }

Appreciate help.

Thanks.

Upvotes: 0

Views: 398

Answers (1)

Odrakir
Odrakir

Reputation: 4254

You create OneViewController as a local variable of the method displayviewsAction. Not as the property you defined previously.

That's why it's not working, the variable gets released once you exit the method.

Change this line:

OneViewController *oneviewcontroller = [[OneViewController alloc] init];

With this one:

self.oneviewcontroller = [[OneViewController alloc] init];

Upvotes: 2

Related Questions