macos 从 NSTextField 获取值

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

Get value from NSTextField

macoscocoanstextfield

提问by anakin

I have an NSTextFieldand I need to get the field's value into a variable. What's the appropriate method?

我有一个NSTextField,我需要将该字段的值放入一个变量中。什么是合适的方法?

回答by toholio

For an NSStringyou would use:

对于NSString您将使用的:

NSString *myString = [theTextField stringValue];

For an intyou would use:

对于int您将使用的:

int myInt = [theTextField intValue];

There are many other methods for getting the value from a control. Have a look at the NSControlreference for more info, under the "Getting and Setting the Control's Value" section.

还有许多其他方法可以从控件中获取值。NSControl“获取和设置控件的值”部分下查看更多信息的参考。

Here's a list:

这是一个列表:

  • doubleValue
  • floatValue
  • intValue
  • integerValue
  • objectValue
  • stringValue
  • attributedStringValue
  • doubleValue
  • floatValue
  • intValue
  • integerValue
  • objectValue
  • stringValue
  • attributedStringValue

回答by Stanley

Swift 3.x Version:

斯威夫特 3.x 版本:

myField.stringValue

回答by NilObject

[myField stringValue]

[myField stringValue]

NSTextFieldinherits from NSControl, and NSControldefines the stringValue/setStringvalue:methods.

NSTextField继承自NSControl,并NSControl定义stringValue/setStringvalue:方法。

回答by The Cappy

Also:

还:

Say you have an object (MyObject) that wants to be be notified when someone types into a NSTextField. In the .h file, MyObjectshould declare it conforms to NSTextFieldDelegate, as in...

假设您有一个对象 ( MyObject),它希望在有人输入NSTextField. 在 .h 文件中,MyObject应声明它符合NSTextFieldDelegate,如...

@interface MyObject : NSObject <NSTextFieldDelegate>

Then you set MyObject as the delegate of the NSTextField

然后将 MyObject 设置为 NSTextField

[myTextField setDelegate:myObject]

[myTextField setDelegate:myObject]

Now, you can find out when something happens in the textfield by implementing methods in MyObject like:

现在,您可以通过在 MyObject 中实现方法来找出文本字段中何时发生的事情,例如:

-(void)controlTextDidEndEditing:(NSNotification *)aNotification;
-(void)controlTextDidChange:(NSNotification *)aNotification;
-(void)controlTextDidBeginEditing:(NSNotification *)aNotification;