我有一個模式視圖,其中倒計時開始時,用戶已經執行了一些操作。在倒計時結束時,模態視圖將關閉。以下是我爲此目標寫的過程,但不幸的是,它導致了一些線程問題。有什麼方法可以用沒有潛在線程問題的方式重寫?倒計時和解除模態視圖
- (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];
}
但問題是,倒計時不再發生。
你能解釋一下「使得一些線程問題」更詳細一點? – lukya
附加的圖像說,你的LanguageSelectionViewController沒有在該類中找到方法changeLanguage。 – NSSwift