2010-08-18 184 views
31

嘿所以我只是做一個示例應用程序,我有一點麻煩,所以我有一個表視圖,然後我有幾行,當用戶點擊一行時,它將他們帶到一個新的視圖。在這個觀點上,我有一個播放音樂的按鈕。我使用計時器根據音樂持續時間和剩餘時間增加滑塊。如何停止NSTimer

現在我的問題是,我必須放什麼,以便當我通過左上角按鈕返回到表格視圖時,NSTimer停止?

這是我迄今爲止,我不能重複:YES計時器停止。

#import "SecondViewController.h" 

@implementation SecondViewController 
@synthesize lbl1; 
@synthesize timer; 

-(IBAction) slide { 
myMusic.currentTime = slider.value; 
} 

-(IBAction)play 
{ 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES]; 
slider.maximumValue = [myMusic duration]; 
myMusic.volume = 0.2; 
[myMusic prepareToPlay]; 
[myMusic play]; 
} 

-(IBAction)pause 
{ 
[myMusic pause]; 
} 

-(IBAction)stop 
{ 
slider.value = 0; 
myMusic.currentTime = 0; 
[myMusic stop]; 
} 

- (void)updateTime{ 
    slider.value = myMusic.currentTime; 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 

    //This plays music, we must give it a path to find the file and then u can change it. 
NSString * pathToMusicFile = [[NSBundle mainBundle] pathForResource:@"Katy" ofType:@"mp3"]; 
    myMusic = [[ AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL]; 
    myMusic.delegate = self; 
    myMusic.numberOfLoops = -1; 

slider.value = 0; 
//[myMusic play]; 

    [super viewDidLoad]; 
} 


- (void)viewDidUnload { 

[timer invalidate]; 
timer = nil; 
//myMusic.currentTime = 0; 
[myMusic stop]; 
[super viewDidUnload]; 

// Release any retained subviews of the main view. 
// e.g. self.myOutlet = nil; 
} 

-(IBAction) TxtChange;{ 

lbl1.text = @" test2! I CHNAGED TEXT FROM ANOTHER XIB"; 
} 

- (void)dealloc { 
[timer invalidate]; 
timer = nil; 
[myMusic release]; 
    [super dealloc]; 
} 

@end 

回答

13

這樣的:

[yourtimername invalidate]; 

但要小心,你不能停止計時,如果它已經停止,因爲您的應用程序會崩潰。

+2

當我使一個未實例化的計時器無效時,應用程序不會崩潰。 – IgniteCoders 2014-03-12 15:30:13

67

使用下面的代碼。它將工作 但請記住,只有當我們的計時器處於運行模式時才能調用它,否則應用程序將崩潰。

- (void)viewWillDisappear:(BOOL)animated { 
    //BEFORE DOING SO CHECK THAT TIMER MUST NOT BE ALREADY INVALIDATED 
    //Always nil your timer after invalidating so that 
    //it does not cause crash due to duplicate invalidate 
     if(timer) 
     { 
     [timer invalidate]; 
     timer = nil; 
     } 
    [super viewWillDisappear:animated]; 
} 
+0

我準備在早上去嘗試一下,上午2點,一直呆在這一天。希望你的解決方案有效。幾個小時後,病人就回到這篇文章。 – cruisx 2010-08-18 05:53:34

+21

有人應該告訴他,他整整晚了3年纔回到你身上......無論哪種方式,解決了我的問題! – 2013-11-24 05:07:58

+0

@JonMattingly lol – 2013-12-05 07:01:35

8

正如iCoder所說,它必須在viewWillDisappear中失效。

我添加了以下內容來確保。這個對我有用。希望它可以幫助(並增加了icoders答案不原意複製)

- (void) startTimer 
{ 
[self stopTimer]; 
timer = [NSTimer scheduledTimerWithTimeInterval: 5.0 
                target: self 
                selector:@selector(onTick) 
                userInfo: nil repeats:YES]; 

} 

- (void) stopTimer 
{ 
    if (timer) { 
     [timer invalidate]; 
     timer = nil; 
    } 

} 
1

技巧是使用一個單獨的類,它主要是用來作爲一個單身 雖然如果妳不希望 谷歌myGlobals類使用NSTimer ,然後

可以說視點1是其中U進入並查看2是一個其中U回去2.

所以在視圖情況下,寫NSTimer無效的viewDidLoad定時器(假設你爲這兩個視圖都有兩個單獨的類s)。問題將發生,因爲當你去查看1時,視圖2定時器將被釋放。所以,做一個Singleton類和保存NSTimer指針這樣

//to be done in viewDidLoad function 
theGlobals *globals = [theGlobals alloc] init];// for the singleton class to initiate, 
[globals.myTimer invalidate]; 


//to be done in viewDidUnload 
theGlobals *globals = [theGlobals alloc] init];// for the singleton class to initiate, 
globals.myTimer = [NSTimer scheduledTimerWithTimeInterval: 5.0 
               target: self 
               selector:@selector(onTick) 
               userInfo: nil repeats:YES]; 

哦,這裏是myGlobals類別代碼的ü將需要

//theglobals.h file 

#import <Foundation/Foundation.h> 


@interface theGlobals : NSObject 
{ 
    NSTimer *myTimer; 
} 

@property (nonatomic, copy) NSString *changeyes; 



+(theGlobals*)sharedInstance; 
@end 

//implementaton .m file 

#import "theGlobals.h" 
@implementation theGlobals 

@synthesize myTimer; 



static theGlobals *sharedInstance; 

- (id) init 
{ 
return self; 
} 

+(theGlobals*)sharedInstance 
{ 
if (!sharedInstance) 
{ 
    sharedInstance = [[theGlobals alloc] init]; 
} 
return sharedInstance; 
} 
@end 
2

你可以使用兩種不同的東西

[timername invalidate]; 

或者您可以使用

timername = nil; 
+0

後者不會實際阻止計時器,第一個計劃已於2010年8月發佈。 – 2014-06-08 21:01:04

6

4歲的帖子,我知道,但也許我可以拯救下一個人浪費時間試圖使NSTimer無效。蘋果文檔解釋它,它適用於我。

在.H

@property(nonatomic, retain) NSTimer *yourTimer; 
@property(weak) NSTimer *repeatingTimer; 

。米

@synthesize yourTimer, repeatingTimer; 

在viewDidLoad中

yourTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimerInterval)(40.0/60.0) 
              target:self 
              selector:@selector(blink) 
              userInfo:nil 
              repeats:TRUE]; 

    self.repeatingTimer = yourTimer; 

在我viewDidDisappear

[repeatingTimer invalidate]; 
    repeatingTimer = nil; 

,關鍵是在第一的NSTimer的分配(弱)的NSTimer對象和殺死的對象。

0

只是使計時器無效

[timer invalidate];