Iphone 如何停止NSTimer

Iphone 如何停止NSTimer,iphone,objective-c,xcode,nstimer,Iphone,Objective C,Xcode,Nstimer,嘿,我只是在做一个示例应用程序,我遇到了一点小麻烦,所以我有一个表视图,然后我有几行,当用户单击一行时,它会将他们带到一个新视图。在这个视图中,我有一个播放音乐的按钮。我正在使用计时器根据音乐持续时间和剩余时间增加滑块 现在我的问题是,当我通过左上角的按钮返回到表视图时,我必须放置什么才能使NSTimer停止 这就是我到目前为止所做的,我无法重复:是的,计时器停止 #import "SecondViewController.h" @implementation SecondViewContro

嘿,我只是在做一个示例应用程序,我遇到了一点小麻烦,所以我有一个表视图,然后我有几行,当用户单击一行时,它会将他们带到一个新视图。在这个视图中,我有一个播放音乐的按钮。我正在使用计时器根据音乐持续时间和剩余时间增加滑块

现在我的问题是,当我通过左上角的按钮返回到表视图时,我必须放置什么才能使NSTimer停止

这就是我到目前为止所做的,我无法重复:是的,计时器停止

#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
像这样:

[yourtimername invalidate];

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

使用下面的代码。它会起作用的 但请记住,只有在计时器处于运行模式时才必须调用它,否则应用程序将崩溃

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

正如iCoder所说,它必须在viewwill中无效

我添加了以下内容只是为了确保。它对我有用。希望它能帮上忙(并补充了icoders的答案“不打算复制”)


诀窍是使用singleton类, GoogleMyGlobals类,主要用作单例 虽然如果您确实希望使用
NSTimer
,但是

假设视图1是您进入的视图,视图2是您返回的视图2

因此,在视图2的情况下,编写
NSTimer
以使
viewDidLoad
中的计时器无效(假设两个视图都有两个单独的类)。问题将出现,因为当您转到视图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
//theglobals.h文件
#进口
@接口全局:NSObject
{
NSTimer*myTimer;
}
@属性(非原子,复制)NSString*changeyes;
+(全球*)共享立场;
@结束
//implementation.m文件
#导入“theGlobals.h”
@全球战略的实施
@合成myTimer;
静态全局*共享状态;
-(id)init
{
回归自我;
}
+(theGlobals*)共享立场
{
如果(!sharedInstance)
{
sharedInstance=[[theGlobals alloc]init];
}
返回共享状态;
}
@结束

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

    [timername invalidate];
或者你可以用

    timername = nil;

4年前的帖子,我知道,但也许我可以避免下一个家伙浪费时间试图使NSTimer无效。苹果的文档解释了这一点,它对我很有用

in.h

英寸

在viewDidLoad中

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

  self.repeatingTimer = yourTimer;
在我看来,我消失了

   [repeatingTimer invalidate];
   repeatingTimer = nil;

诀窍在于将第一个NSTimer赋值给(弱)NSTimer对象并杀死该对象。

只需使计时器无效即可


[计时器失效]

我打算早上2点去试试这个,整天都在忙这个。希望你的解决方案能奏效。几个小时后我会回到这篇文章。应该有人告诉他,他在这件事上迟到了三年左右。。。不管怎样,都解决了我的问题!那么@cruisx,你的问题解决了吗?在viewwill中停止计时器时要非常小心。如果您的应用进入后台,则不会调用此函数。在这种情况下,您可能希望停止计时器,以避免操作系统杀死您的应用程序。当我使未实例化的计时器无效时,应用程序不会崩溃。后一个计时器实际上不会停止计时器,第一个计时器已于2010年8月发布。
@synthesize yourTimer, repeatingTimer;
yourTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimerInterval)(40.0/60.0)
                                             target:self
                                           selector:@selector(blink)
                                           userInfo:nil
                                            repeats:TRUE];

  self.repeatingTimer = yourTimer;
   [repeatingTimer invalidate];
   repeatingTimer = nil;