2010-08-18 105 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];