Search code examples
objective-cmacoscocoansstringnstextfield

NSTextField not updating in setStringValue


I have an NSTextField that displays status info as needed. It works, sort of... The problem is that it only displays the last very last updatedString and none before it in the NSTextField. When checking NSLog all of the strings appear. Is NSTextFieldunable to process it quick enough or maybe it's displaying so fast the human eye can't see it?

example:

- (void)update {

            NSString* updatedString = @"Update - 1";
            [self updateTextField:updatedString];
            // do other stuff
            updatedString = @"Update - 2";
            [self updateTextField:updatedString];
            // do other stuff
            updatedString = @"Update - 3";
            [self updateTextField:updatedString];
            // do other stuff
            updatedString = @"Update - 4";
            [self updateTextField:updatedString];
}

- (void)updateTextField: (NSString *)updatedString  {

        [TextFieldValue setStringValue:[NSString stringWithFormat:@"%@", updatedString]];   
}

NSTextField shows (once):

Update - 4

NSLog shows:

Update - 1
Update - 2
Update - 3
Update - 4

Solution

  • you can append the string in text field by

    [TextFieldValue setStringValue:[NSString stringWithFormat:@"%@%@", [TextFieldValue stringValue], updatedString]];
    

    if you want it to sleep, use

     CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1/*seconds*/, NO);
    

    instead of sleep so your app won't be freezed