我想模仿iphone解鎖密碼對話框。我創建了四個標籤,在用戶輸入第四個數字後,我檢查了正確性。如果正確,我會解散對話框。如果不正確,我會清除輸入框並讓用戶重試。當按下按鈕時,我撥打updateText
。如何在重置之前更新密碼標籤?
- (void) updateText{
pw1.text = [self.enteredPwd objectAtIndex:0];
pw2.text = [self.enteredPwd objectAtIndex:1];
pw3.text = [self.enteredPwd objectAtIndex:2];
pw4.text = [self.enteredPwd objectAtIndex:3];
if (currentIndex > 3) {
// check pwd
[self checkPwd];
}
}
- (IBAction)pwBtnUp:(id)sender {
UIButton *btn = (UIButton*)sender;
[self.enteredPwd insertObject:[NSString stringWithFormat:@"%d", btn.tag] atIndex:currentIndex++];
[self updateText];
}
但是我有一個問題:第四個字符從來沒有機會顯示。無論是否正確,點擊最後一個按鈕後立即解除/清除對話框。
我不知道如何解決這個問題。我甚至嘗試用完成塊中調用的checkPwd更新UIView Animation中的標籤,但沒有運氣。
請給我一些建議如何解決這個問題。在清除標籤之前,蘋果的對話框停留大約0.5秒。
感謝
利奧
我試過這段代碼,但它崩潰了「無法識別的選擇器checkPwd」。我用錯了嗎? 'NSTimer * checkPwdTimer = [NSTimer timerWithTimeInterval:.5 target:self selector:@selector(checkPwd :) userInfo:nil repeats:NO]; NSRunLoop * myRunLoop = [NSRunLoop currentRunLoop]; [myRunLoop addTimer:checkPwdTimer forMode:NSDefaultRunLoopMode];'我有一個' - (void)checkPwd'來檢查密碼 – leo
OK。在我將@selector(checkPwd :)更改爲@selector(checkPwd)之後它正在工作。我不知道有什麼區別。蘋果文檔在方法名後面有冒號。 – leo
如果你在checkPwd後面有一個參數(你可能使用帶有(id)sender的IBAction),你需要冒號來表示應該調用的選擇器有一個參數。 – LuckyLuke