嘿所以我只是做一个示例应用程序,我有一点麻烦,所以我有一个表视图,然后我有几行,当用户点击一行时,它将他们带到一个新的视图。在这个观点上,我有一个播放音乐的按钮。我使用计时器根据音乐持续时间和剩余时间增加滑块。如何停止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
当我使一个未实例化的计时器无效时,应用程序不会崩溃。 – IgniteCoders 2014-03-12 15:30:13