【发布时间】:2015-03-23 11:43:51
【问题描述】:
我是 iOS 新手, 在我的项目中,我将 NSTimer 与导航控制器一起使用。 我在我的项目中使用了两个类, 第 1 类是 ViewController,第 2 类是 PlayTheme。 ViewController 和 PlayTheme 类与 segue 相连。 在 PlayTheme 类中,我使用 NSTimer 和“FiredTimer 方法”调用每 10 毫秒。 PlayTheme类的源代码是: 下面是 NSTimer 启动的方法
- (IBAction)startTimerMethod:(id)sender
{
UIBackgroundTaskIdentifier bgTask =0;
UIApplication *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
timer = [NSTimer
scheduledTimerWithTimeInterval:0.01
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:YES];
}
下面的方法是停止计时器
- (IBAction)stopTimerMethod:(id)sender
{
if([timer isValid])
{
[timer invalidate];
timer=Nil;
}
}
两种方法都有效,但是当我按照以下步骤操作时,计时器不会停止:
- 我在 PlayTheme 类和 StartTime 中
- 返回 ViewController
- 回到 PlayTheme 课程
- 和StopTimer方法调用,方法调用但不停止定时器
给我建议以解决我的问题,并告诉我 NSTimer 如何在 BackGround 中使用 NSTimer 播放特定时间的声音?
谢谢你。
【问题讨论】:
标签: ios objective-c nstimer avaudioplayer navigationcontroller