macos 以编程方式更改 NSTextField 中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4769247/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:38:46  来源:igfitidea点击:

Change the text in a NSTextField programmatically?

cocoamacosnsstringnstextfield

提问by Conor Taylor

I am used to doing this in Cocoa-touch, so I assumed it would be quite simple. I have already tried:

我习惯于在 Cocoa-touch 中这样做,所以我认为它会很简单。我已经尝试过:

field.text = [nsstring stringwithformat:@"%i", number];
[field setText:@"%i", number];

and setString, all to no avail.

和setString,都无济于事。

回答by grahamparks

This will work:

这将起作用:

[field setStringValue:[NSString stringWithFormat:@"%i",number]];

If it doesn't do anything, it probably means fieldisn't set to point to the field (e.g. by making a connection in Interface Builder).

如果它没有做任何事情,则可能意味着field未设置为指向该字段(例如,通过在 Interface Builder 中建立连接)。

回答by John Parker

You should be able to use the setStringValue: method that NSTextFieldinherits from NSControl.

您应该能够使用NSTextFieldNSControl继承的 setStringValue: 方法。

The full signature is:

完整的签名是:

- (void)setStringValue:(NSString *)aString