Xcode当UITextField与UILabel具有相同的文本时,如何使用if语句停止计时器标签

时间:2012-06-14 20:25:59

标签: xcode if-statement timer uitextfield uilabel

我一直试图在应用程序上做这个很长一段时间,但我无法让它工作,请有人帮助我并发布一些代码我会怎么做。我需要做的是当UITextField中的文本等于UILabel时。感谢

小时。的ViewController

@interface AlphabetSceneViewController : UIViewController {

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

IBOutlet UILabel *wordToType;
IBOutlet UITextField *wordTyped;

和m。查看控制器

    - (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"]) {

    }

}

非常感谢!

1 个答案:

答案 0 :(得分: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];
    }
}