ios - NSTimer 倒计时无法正常工作
<p><p>我正在尝试区分时间并在 ui 标签中向用户显示倒数计时器</p>
<p><code>NSTimer</code>的声明</p>
<pre><code>@property (strong, nonatomic)NSTimer *timer;
</code></pre>
<p>这是我的计时器,确实加载了</p>
<pre><code>_timer = ;
</code></pre>
<p>这是我的 updateCountdown 方法</p>
<pre><code>-(void) updateCountdown:(int)secondsLeft {
int hours, minutes, seconds;
secondsLeft--;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
_countDownlabel.text = ;///;
NSLog(@"%@",);
if ( secondsLeft == 0 ) {
;
}
}
</code></pre>
<p>我的日志详细信息是</p>
<pre><code>2017-06-30 09:49:34.070 Barebones requestReply cust liqour category: {
date = "30-6-2017";
"end_time" = "11:41";
"market_crash" = true;
"start_time" = "09:41";
}
2017-06-30 09:49:34.070 Barebones true
2017-06-30 09:49:34.070 Barebones 09:41
2017-06-30 09:49:34.070 Barebones 11:41
2017-06-30 09:49:34.070 Barebones 30-6-2017
2017-06-30 09:49:34.073 Barebones 2016-12-25 08:58:00 +0000
2017-06-30 09:49:34.073 Barebones 2016-12-25 12:15:00 +0000
2017-06-30 09:49:34.073 Barebones 197.000000 is the timedifference
2017-06-30 09:49:34.073 Barebones 00:03:17
2017-06-30 09:49:35.075 Barebones 991:05:35
2017-06-30 09:49:36.075 Barebones 991:05:35
2017-06-30 09:49:37.075 Barebones 991:05:35
2017-06-30 09:49:38.075 Barebones 991:05:35
</code></pre>
<p>这个值继续执行</p>
<p>目标:- 倒计时到零并停止计时器实际上一旦倒计时结束我将隐藏标签</p>
<p>更新:-</p>
<pre><code> int secondsLeft=/60];
</code></pre>
<p>上面的定时器已经初始化了</p>
<p>这是我更新的计时器:-</p>
<pre><code>int secondsLeft=/60;
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
, @"cID", nil];
_timer = ;
</code></pre>
<p>这是我更新的计时器方法:-</p>
<pre><code>- (void)updateCountdown:(NSTimer *)timer{
int hours, minutes, seconds;
NSDictionary *userInfo = ;
int secondsLeft = [ intValue];
secondsLeft--;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
_countDownlabel.text = ;///;
NSLog(@"%@",);
if ( secondsLeft == 0 ) {
;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>几件事。</p>
<ol>
<li><p>正如 vadian 所说,如果您使用基于选择器的计时器,则计时器处理函数采用单个参数,该参数是对计时器本身的引用。如果你想跟踪倒计时,你可以定义属性来跟踪它:</p>
<pre><code>@interface ViewController ()
@property (nonatomic, weak) IBOutlet UILabel *label;
@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, strong) NSDateComponentsFormatter *formatter;
@property (nonatomic, strong) NSDate *stopTime;
@end
@implementation ViewController
- (void)viewDidLoad {
;
self.formatter = [ init];
self.formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
self.formatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
self.formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
self.stopTime = [ dateByAddingTimeInterval:5 * 60]; // in 5 minutes, for example
self.timer = ;
; // don't wait one second before firing the first time; fire now
}
- (void)handleTimer:(NSTimer *)timer {
NSDate *now = ;
if ( == NSOrderedDescending) {
// do whatever you want when timer stops
;
return;
}
self.label.text = ;
}
// Note, when the view disappears, invalidate the timer so the timer doesn't
// keep strong reference to the view controller. Note that in this selector-based
// pattern, I can't attempt to do this in `dealloc`, because the scheduled timer
// will keep a strong reference, preventing `dealloc` from getting called. So do
// this in `viewDidDisappear`.
- (void)viewDidDisappear:(BOOL)animated {
;
}
@end
</code></pre> </li>
<li><p>如果只支持 iOS 10 及更高版本,我建议使用完成 block 计时器,因为它可以进一步简化流程:</p>
<pre><code>@interface ViewController ()
@property (nonatomic, weak) IBOutlet UILabel *label;
@property (nonatomic, weak) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
;
NSDateComponentsFormatter *formatter = [ init];
formatter.allowedUnits = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorPad;
NSDate *stopTime = [ dateByAddingTimeInterval:5 * 60];// in 5 minutes, for example
typeof(self) __weak weakSelf = self; // make sure to not reference `self` in block below, but only reference `weakSelf` to avoid timer from maintaining strong reference
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:true block:^(NSTimer * _Nonnull timer) {
NSDate *now = ;
if ( == NSOrderedDescending) {
// do whatever you want when timer stops
;
return;
}
weakSelf.label.text = ;
}];
; // don't wait one second before firing the first time; fire now
}
// Because I was careful to not reference `self` inside the above block,
// this block-based timer will NOT keep a strong reference to the view
// controller. Nonetheless, when the view controller is dismissed, I want
// to stop the timer, to avoid wasting CPU cycles on a timer that isn't
// needed anymore.
- (void)dealloc {
;
}
@end
</code></pre>
<p>显然,如果您还需要支持 iOS 9 及更早版本,则必须使用上述基于选择器的解决方案。</p></li>
<li><p>我建议不要依赖定时器来调整时间,因为你不能保证定时器会以你想要的频率被调用。在上面的两个例子中,我捕捉到我倒计时的时间,并且只显示“现在”和预定的“停止时间”之间的时间量。</p></li>
<li><p>注意,我还建议您不要自己构建格式字符串。有一个方便的 <code>NSDateComponentsFormatter</code> 可以为你格式化。如果可以,请使用它。</p></li>
<li><p>您已将计时器引用 <code>strong</code>。您可以将其设置为 <code>weak</code>,因为计划的计时器在 <code>invalidated</code> 之前不会被释放。一旦它被<code>invalidated</code>,它可以很方便地自动为你解除分配。</p></li>
</ol></p>
<p style="font-size: 20px;">关于ios - NSTimer 倒计时无法正常工作,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44838584/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44838584/
</a>
</p>
页:
[1]