如何阻止NSTimer

时间:2010-08-18 04:41:03

标签: iphone objective-c xcode 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

6 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:13)

像这样:

[yourtimername invalidate];

但要小心,如果计时器已经停止,你就不能停止计时器,因为你的应用程序会崩溃。

答案 2 :(得分: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;
    }

}

答案 3 :(得分:2)

你可以使用两种不同的东西

    [timername invalidate];

或者你可以使用

    timername = nil;

答案 4 :(得分:1)

Trick是使用单例类, 谷歌myGlobals类,主要用作单身人士 虽然如果您确实希望使用NSTimer,那么

让我们说视图1是你进入的,而视图2是你回到2的那个。

因此,在视图2的情况下,编写NSTimer以使viewDidLoad中的计时器无效(假设两个视图都有两个单独的类)。问题将发生,因为当你去查看1时,视图2计时器将是dealloc。所以,创建一个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

答案 5 :(得分:0)

只是使计时器无效

[计时器无效];