简体   繁体   中英

Xcode cocoa osx NSTextfield query

I created a simple maths based app. The app asks a user 6 multiplication table questions. eg Q1 10 x 10 The user enters an answer The app displays whether the user's answer was correct or incorrect and displays this using the following

IBOutlet NSTextField *CorrectIncorrect;

In a cycle of the app (1 question of 6) CorrectIncorrect is used to display the strings 'correct' or 'incorrect' using this line of code

[CorrectIncorrect setStringValue:receivedAnswer];
[[CorrectIncorrect window] display];

Then as the next question is posed to the user, the either string is cleared using the following code.

[CorrectIncorrect setStringValue:@""];
[[CorrectIncorrect window] display];

Initially, each CorrectIncorrect string was being cleared to quickly, so the user never saw if their answer was 'correct' or 'incorrect'. I therefore used a time delay method to slow the process in order to allow the user to see the display before it was cleared.see below

- (void)TimeDelay
{
    startInterval = [NSDate timeIntervalSinceReferenceDate];
    stopInterval = [NSDate timeIntervalSinceReferenceDate];
    while ((stopInterval - startInterval) <= 1)
    {

    stopInterval = [NSDate timeIntervalSinceReferenceDate];
    }
}

This worked fine in Xcode 5. Does not work in Xcode 7.3.1. Any advice appreciated.

Few things:

1) Time interval is in seconds, so <= 1 really isn't much time, have you tried increasing that time and seeing what it looks like?

2) You should also consider adding a button like "Go to next question". That way you won't need to have a timer

3) If you want to have a timer to reset the string, you should just use Grand Central Dispatch

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [CorrectIncorrect setStringValue:@""];
});

one-ish line, and is more intuitive

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM