1

我一直在尝试在应用程序上执行此操作,但我无法使其正常工作,请有人帮助我并发布一些关于我将如何执行此操作的代码。我需要发生的是当 UITextField 中的文本等于 UILabel 时。谢谢

H。视图控制器

@interface AlphabetSceneViewController : UIViewController {

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

IBOutlet UILabel *wordToType;
IBOutlet UITextField *wordTyped;

还有他们。视图控制器

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

    }

}

非常感谢帮助!

4

1 回答 1

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];
    }
}
于 2012-06-17T08:33:38.783 回答