macos 你如何在 NSTextField 中设置文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2841559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set the text in an NSTextField?
提问by Alexsander Akers
I'm trying to set the text in an NSTextField, but the -setStringValue:
and -setTitleWithMnemonic:
methods are not working. Any ideas?
我正在尝试在 NSTextField 中设置文本,但-setStringValue:
和-setTitleWithMnemonic:
方法不起作用。有任何想法吗?
回答by 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.)
setStringValue:
是这样做的方法。您应该确保您的插座设置正确。(在调试器中,确保您的文本字段变量不为空。)
回答by Jim
Just do something like this:
只是做这样的事情:
myLabel.stringValue = @"My Cool Text";
回答by Alexander
Just myLabel.stringValue = "MY TEXT"
works here, using Swift and Xcode 6.
只是myLabel.stringValue = "MY TEXT"
在这里工作,使用 Swift 和 Xcode 6。
回答by Maulik Rajani
Swift 4
斯威夫特 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)
注意:从 self.yourTextField.stringValue 中获取值会得到警告消息,即
为了避免这种警告,你可以像这样使用(建议的方式)
DispatchQueue.main.async {
your code ...
}
OR also refer to this.
或者也参考这个。
回答by Jon Schneider
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.
该integerValue
属性是在NSTextField
的父类上定义的NSControl
。有关它支持的方法的完整列表,请参阅链接的文档页面。
回答by Alp Altunel
ObjectiveC:
目标C:
[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"]]];
回答by Joe Manto
just do this
就这样做
[textField setString:@"random"];