嘿所以我只是做一個示例應用程序,我有一點麻煩,所以我有一個表視圖,然後我有幾行,當用戶點擊一行時,它將他們帶到一個新的視圖。在這個觀點上,我有一個播放音樂的按鈕。我使用計時器根據音樂持續時間和剩餘時間增加滑塊。如何停止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