How do you set the text in an NSTextField?

点点圈 提交于 2019-12-17 18:09:12

问题


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


回答1:


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




回答2:


Just do something like this:

myLabel.stringValue = @"My Cool Text";



回答3:


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




回答4:


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.




回答5:


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



回答6:


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.




回答7:


just do this

[textField setString:@"random"];


来源:https://stackoverflow.com/questions/2841559/how-do-you-set-the-text-in-an-nstextfield

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