How do you set the text in an NSTextField?

旧巷老猫 提交于 2019-11-28 19:02:31
Ken Aspeslagh

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.)

Jim

Just do something like this:

myLabel.stringValue = @"My Cool Text";
Alexander

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

Swift 4

self.yourTextField.stringValue = "Your_Value"

Note: Fetching value from self.yourTextField.stringValue at that will get warning message i.e.

To avoid this kind of warning you can use like this (suggested way)

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

OR also refer to this.

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"]]];

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.

just do this

[textField setString:@"random"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!