2015-07-10 40 views
1

我有一個模式視圖,其中倒計時開始時,用戶已經執行了一些操作。在倒計時結束時,模態視圖將關閉。以下是我爲此目標寫的過程,但不幸的是,它導致了一些線程問題。有什麼方法可以用沒有潛在線程問題的方式重寫?倒計時和解除模態視圖

- (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告訴我關於這個問題

output

更多編輯:

我的代碼已經改變,以反映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

你能解釋一下「使得一些線程問題」更詳細一點? – lukya

+0

附加的圖像說,你的LanguageSelectionViewController沒有在該類中找到方法changeLanguage。 – NSSwift

回答

1

static int i = 3;放置在您的方法countDown之外,您做它的方式會在每次調用該方法時聲明新變量。

+0

重新定位'static int i = 3;'不能解決問題。 [靜態局部變量的值在方法調用中被記住。](http://stackoverflow.com/questions/1087061/whats-the-meaning-of-static-variables-in-an-implementation-of-an-interface ) –

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--; 

}

+0

'定時器。userInfo = @ {@「counter」:@(counter)};'導致錯誤 –

+1

我使用靜態變量編輯代碼 – vadian

+0

感謝您簡化我的代碼,但線程問題仍然存在。 –