How do you set the text in an NSTextField?

CocoaMacosNstextfield

Cocoa Problem Overview


I'm trying to set the text in an NSTextField, but the -setStringValue: and -setTitleWithMnemonic: methods are not working. Any ideas?

Cocoa Solutions


Solution 1 - Cocoa

setStringValue: is the way to do it. You should make sure your outlet is being set properly. (In the debugger, make sure your textfield variable is not null.)

Solution 2 - Cocoa

Just do something like this:

myLabel.stringValue = @"My Cool Text";

Solution 3 - Cocoa

Just myLabel.stringValue = "MY TEXT" works here, using Swift and Xcode 6.

Solution 4 - Cocoa

Swift 4

self.yourTextField.stringValue = "Your_Value"

Note: Fetching value from self.yourTextField.stringValue at that will get warning message i.e. Warning Message To avoid this kind of warning you can use like this (suggested way)

DispatchQueue.main.async {
 your code ... 
} 

OR also refer to this.

Solution 5 - Cocoa

If the value you're trying to set happens to be an integer rather than a string, you don't need to convert the integer value to a string manually; you can just call:

myLabel.integerValue = i;

The integerValue property is defined on NSTextField's parent class, NSControl. See that linked documentation page for a full list of methods it supports.

Solution 6 - Cocoa

ObjectiveC:

[label setStringValue: @"I am a label"];

original code I use in my code to display application version is:

    [lblVersion setStringValue:[NSString stringWithFormat:@"v%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]]];

Solution 7 - Cocoa

just do this

[textField setString:@"random"];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexsander AkersView Question on Stackoverflow
Solution 1 - CocoaKen AspeslaghView Answer on Stackoverflow
Solution 2 - CocoaJimView Answer on Stackoverflow
Solution 3 - CocoaAlexanderView Answer on Stackoverflow
Solution 4 - CocoaMaulik RajaniView Answer on Stackoverflow
Solution 5 - CocoaJon SchneiderView Answer on Stackoverflow
Solution 6 - CocoaAlp AltunelView Answer on Stackoverflow
Solution 7 - CocoaJoe MantoView Answer on Stackoverflow