cru*_*isx 32 iphone xcode objective-c 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
Run Code Online (Sandbox Code Playgroud)
Sur*_*rma 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];
}
Run Code Online (Sandbox Code Playgroud)
Dai*_*ggy 13
像这样:
[yourtimername invalidate];
Run Code Online (Sandbox Code Playgroud)
但要小心,如果计时器已经停止,你就不能停止计时器,因为你的应用程序会崩溃.
正如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;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
4岁的帖子,我知道,但也许我可以保存下一个人免于浪费时间尝试使NSTimer无效.苹果文档解释了它,它适用于我.
在.h
@property(nonatomic, retain) NSTimer *yourTimer;
@property(weak) NSTimer *repeatingTimer;
Run Code Online (Sandbox Code Playgroud)
在.m
@synthesize yourTimer, repeatingTimer;
Run Code Online (Sandbox Code Playgroud)
在viewDidLoad中
yourTimer = [NSTimer scheduledTimerWithTimeInterval:(NSTimerInterval)(40.0/60.0)
target:self
selector:@selector(blink)
userInfo:nil
repeats:TRUE];
self.repeatingTimer = yourTimer;
Run Code Online (Sandbox Code Playgroud)
在我的viewDidDisappear中
[repeatingTimer invalidate];
repeatingTimer = nil;
Run Code Online (Sandbox Code Playgroud)
诀窍在于将第一个NSTimer分配给(弱)NStimer对象并杀死该对象.