2012-06-14 35 views
1

我一直試圖在應用程序上執行此操作很長時間,但我無法使其工作,請有人幫忙我併發布了一些關於如何去做的代碼。我需要發生的是當UITextField中的文本與UILabel相等時。謝謝Xcode如何停止計時器如果UITextField具有與UILabel相同的文本,則使用if語句標記

h。的ViewController

@interface AlphabetSceneViewController : UIViewController { 

UILabel *stopWatchLabel; 
NSTimer *stopWatchTimer; 
NSDate *startDate; 

IBOutlet UILabel *wordToType; 
IBOutlet UITextField *wordTyped; 

和米。 view controller

- (void)updateTimer 
{ 
    NSDate *currentDate = [NSDate date]; 
    NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate]; 
    NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval]; 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"mm:ss.SSS"]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]]; 
    NSString *timeString=[dateFormatter stringFromDate:timerDate]; 
    stopWatchLabel.text = timeString; 
} 

- (IBAction)onStartPressed:(id)sender { 
    startDate = [NSDate date]; 

    stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 
                 target:self 
                selector:@selector(updateTimer) 
                userInfo:nil 
                repeats:YES]; 
} 

- (IBAction)onStopPressed:(id)sender {  

    [stopWatchTimer invalidate]; 
    stopWatchTimer = nil; 
    [self updateTimer]; 

    if ([wordToType.text isEqualToString:@"stop"]) { 

    } 

} 

非常感謝!

+0

你能解釋一下什麼叫你的兩個IBAction方法嗎? – 2012-06-15 08:38:53

回答

1

我想出瞭如何做到這一點,我會爲任何想知道的人提供代碼。它只是.m文件的代碼,並且警報視圖只是一些額外的代碼,以便在用戶在計時器停止時顯示警報。

- (IBAction)stopTimer:(id)sender { 

    if([wordTyped.text isEqualToString:wordToType.text]){ 

     [stopWatchTimer invalidate]; 
     stopWatchTimer = nil; 
     [self updateTimer]; 


     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Nice Work" message:[NSString stringWithFormat:@"Good Job! You're time to type the alphabet was %@. Let's see if you can do better...", stopWatchLabel.text] 
                 delegate:self cancelButtonTitle:@"Main Menu" otherButtonTitles: nil]; 

     [alert show]; 
    } 
}