2013-02-23 61 views
0

myTimer在OneViewController中定義,我試圖無效,並從mainviewcontroller再次觸發它,但它不工作。我在這裏錯過了什麼。invalidate和fire從不同的視圖控制器計時器不工作

這裏是我的代碼

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]; 

} 

欣賞的幫助。

謝謝。

回答

2

您創建OneViewController作爲方法displayviewsAction的局部變量。不是您之前定義的屬性。

這就是爲什麼它不工作,一旦退出該方法,變量就會被釋放。

改變這一行:

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

有了這一個:

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

感謝它固定我的問題的一半,現在無效的作品,但大火仍然沒有工作 – user1452248 2013-02-23 19:01:20

+0

當我恢復,那麼它應該火myTimer再次從它被暫停的地方。但它不起作用。任何線索 – user1452248 2013-02-23 19:03:59

+1

我認爲invalidate刪除並釋放計時器。你必須重新創建它。 – Odrakir 2013-02-23 19:10:00

相關問題