我有一个模态视图,当用户执行某些操作时,倒计时开始。在倒计时结束时,模态视图将关闭。以下是我为此目标编写的程序,但不幸的是,它导致了一些线程问题。有没有办法以一种没有潜在线程问题的方式重写它?
- (void)countDown {
static int i = 3;
if (i == 3) {
i--;
UIImage *three = [UIImage imageNamed:@"Three"];
countDownFlag = [[UIImageView alloc] initWithImage:three];
countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
[self.view addSubview:countDownFlag];
[self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
} else if (i == 2) {
i--;
UIImage *two = [UIImage imageNamed:@"Two"];
[countDownFlag setImage:two];
[self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
} else if (i == 1) {
i--;
UIImage *one = [UIImage imageNamed:@"One"];
[countDownFlag setImage:one];
[self performSelector:@selector(countDown) withObject:nil afterDelay:0.5];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
编辑:图片显示了XCode告诉我的问题
更多编辑
我的代码已更改为反映vadian的答案。这是更新
- (void)startCountDown {
NSLog(@"starting count down");
counter = 3;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}
- (void)countDown:(NSTimer *)timer {
if (counter == 3) {
countDownFlag = [[UIImageView alloc] init];
countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:countDownFlag];
});
} else if (counter == 0) {
[timer invalidate];
[self dismissViewControllerAnimated:YES completion:nil];
return;
}
NSArray *imageNameArray = @[@"", @"One", @"Two", @"Three"];
UIImage *image = [UIImage imageNamed:imageNameArray[counter]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.countDownFlag setImage:image];
});
counter--;
}
我新猜测问题可能在于调用倒计时的代码。这是执行此操作的代码。
- (void)changeLanguage:(BOOL) isMyanmar {
if (hasRun == YES) {
return;
}
hasRun = YES;
NSUserDefaults * userdefaults = [NSUserDefaults standardUserDefaults];
[userdefaults setBool:isMyanmar forKey:@"myanmar"];
[userdefaults synchronize];
[self updateCurrentLanguageText];
if ([self isMyanmar]) {
[self changeLanguageFlag:@"MyanmarFlagBig"];
} else {
[self changeLanguageFlag:@"UnitedKingdomFlagBig"];
}
//>>>>>>>>>>>>>>>>>>>> the problem is probably here
[self startCountDown];
}
更改语言代码后运行的任何代码都将失败。可能存在问题。
编辑: 线程问题已经消失,但倒计时不再发生了。
在我将changeLanguage
中的代码移动到调用它的方法之后,问题神秘地消失了。 (重复但它有效。)
- (void)changeLanguageToEnglish {
[theLock lock];
if (hasRun == YES) {
[theLock unlock];
return;
}
hasRun = YES;
[userdefaults setBool:false forKey:@"myanmar"];
[userdefaults synchronize];
[self updateCurrentLanguageText];
if ([self isMyanmar]) {
[self changeLanguageFlag:@"MyanmarFlagBig"];
} else {
[self changeLanguageFlag:@"UnitedKingdomFlagBig"];
}
[self startCountDown];
[theLock unlock];
}
- (void)changeLanguageToMyanmar {
[theLock lock];
if (hasRun == YES) {
[theLock unlock];
return;
}
hasRun = YES;
[userdefaults setBool:true forKey:@"myanmar"];
[userdefaults synchronize];
[self updateCurrentLanguageText];
if ([self isMyanmar]) {
[self changeLanguageFlag:@"MyanmarFlagBig"];
} else {
[self changeLanguageFlag:@"UnitedKingdomFlagBig"];
}
[self startCountDown];
[theLock unlock];
}
但问题是倒计时不再发生了。
答案 0 :(得分:2)
您可以使用NSTimer执行倒计时和计数器的静态变量。
方法startCountDown
启动计时器。
方法countDown:(NSTimer *)timer
每0.5秒调用一次。传递的NSTimer参数正是已启动的计时器。
它根据其值执行操作并减少计数器。如果计数器为0,则计时器无效,控制器解除。
static UInt8 counter = 0;
- (void)startCountDown {
countDownFlag = [[UIImageView alloc] init];
countDownFlag.frame = CGRectMake(0, 370, countDownFlag.frame.size.width, countDownFlag.frame.size.height);
countDownFlag.center = CGPointMake(width / 2, countDownFlag.center.y);
[self.view addSubview:countDownFlag];
counter = 3;
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(countDown:) userInfo:nil repeats:YES];
}
- (void)countDown:(NSTimer *)timer {
if (counter == 0) {
[timer invalidate];
[self dismissViewControllerAnimated:YES completion:nil];
return;
}
NSArray *imageNameArray = @[@"", @"One", @"Two", @"Three"];
UIImage *image = [UIImage imageNamed:imageNameArray[counter]];
dispatch_async(dispatch_get_main_queue(), ^{
[self.countDownFlag setImage:image];
});
counter--;
}
答案 1 :(得分:1)
将static int i = 3;
放在方法countDown之外,就像你每次调用该方法时声明新变量一样。