如何阻止NSTimer [英] How to stop NSTimer

查看:85
本文介绍了如何阻止NSTimer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them to a new view. On this view i have a button to play music. I am using a timer to make a slider bar increase based on music duration and remaining time.

现在我的问题是,当我走的时候,我该怎么做通过左上角按钮返回表格视图,NSTimer会停止吗?

Now my problem is, what do i have to put so that when i go back to the table view via the top left button, that the NSTimer stops?

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

this is what i have so far, i cannot get a repeat: YES timer to stop.

#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


推荐答案

使用以下代码。它将工作
但请记住,只有在我们的计时器处于运行模式时才能调用它,否则应用程序将崩溃。

Use the below code. It will work But keep in mind that it must only be called if our timer is in running mode else the application will get crashed.

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

这篇关于如何阻止NSTimer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆