Ios NSTimer:切换视图后更新标签不';行不通
我想使用NSTimer作为倒计时。柜台工作正常;-)! 但我切换到另一个viewController,计时器不会在“前台”运行-因此标签不会更新。。。。回到这个观点之后 我的NSLOG显示计时器仍在运行,如我所愿;-) 那么,我必须以哪种方式工作,我的计时器更新我的标签 代码如下:Ios NSTimer:切换视图后更新标签不';行不通,ios,objective-c,label,progress-bar,nstimer,Ios,Objective C,Label,Progress Bar,Nstimer,我想使用NSTimer作为倒计时。柜台工作正常;-)! 但我切换到另一个viewController,计时器不会在“前台”运行-因此标签不会更新。。。。回到这个观点之后 我的NSLOG显示计时器仍在运行,如我所愿;-) 那么,我必须以哪种方式工作,我的计时器更新我的标签 代码如下: - (IBAction)startClock:(id)sender { globalTimer = 20; timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- (IBAction)startClock:(id)sender {
globalTimer = 20;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateCircularProgressBar)
userInfo:nil
repeats:YES];
}
- (void)updateCircularProgressBar
{
// Values to be passed on to Circular Progress Bar
if (globalTimer > 0 && globalTimer <= 1200) {
globalTimer--;
minutesLeft = globalTimer / 60;
secondsLeft = globalTimer % 60;
[self drawCircularProgressBarWithMinutesLeft:minutesLeft secondsLeft:secondsLeft];
NSLog(@"Time left: %02d:%02d", minutesLeft, secondsLeft);
}
else {
[self drawCircularProgressBarWithMinutesLeft:0 secondsLeft:0];
[timer invalidate];
}
}
-(iAction)StartLock:(id)发送方{
globalTimer=20;
计时器=[NSTimer scheduledTimerWithTimeInterval:1.0
目标:自我
选择器:@selector(updateCircularProgressBar)
用户信息:无
重复:是];
}
-(无效)updateCircularProgressBar
{
//要传递到循环进度条的值
如果(globalTimer>0&&globalTimer也许您使用dispatch_uu方法的运气会更好,如下面的回答:对不起,我不太明白…那么,您使用的是两个视图控制器?您正在谈论的标签在哪里?在两个视图控制器上?如何在视图控制器之间导航?到底是什么问题?更新了代码…;-)我使用不同的控制器(在一个控制器中我使用这个计时器)和一个带有CircularProgressTimer的类。。。
/*-------------------- TIMER --------------------*/
// Draws the progress circles on top of the already painted background
- (void)drawCircularProgressBarWithMinutesLeft:(NSInteger)minutes secondsLeft:(NSInteger)seconds
{
// Removing unused view to prevent them from stacking up
for (id subView in [self.view subviews]) {
if ([subView isKindOfClass:[CircularProgressTimer class]]) {
[subView removeFromSuperview];
}
}
// Init our view and set current circular progress bar value
CGRect progressBarFrame = CGRectMake(0, 0, 180, 180);
progressTimerView = [[CircularProgressTimer alloc] initWithFrame:progressBarFrame];
[progressTimerView setCenter:CGPointMake(160, 210)];
[progressTimerView setPercent:seconds];
if (minutes == 0 && seconds <= 0) {
[progressTimerView setInstanceColor:[UIColor magentaColor]];
}
// Here, setting the minutes left before adding it to the parent view
[progressTimerView setMinutesLeft:minutesLeft];
[progressTimerView setSecondsLeft:secondsLeft];
[self.view addSubview:progressTimerView];
progressTimerView = nil;
}