Get value from NSTextField

試著忘記壹切 提交于 2019-11-28 16:36:09
toholio

For an NSString you would use:

NSString *myString = [theTextField stringValue];

For an int you would use:

int myInt = [theTextField intValue];

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

Here's a list:

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

[myField stringValue]

NSTextField inherits from NSControl, and NSControl defines the stringValue/setStringvalue: methods.

Swift 3.x Version:

myField.stringValue
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, MyObject should declare it conforms to NSTextFieldDelegate, as in...

@interface MyObject : NSObject <NSTextFieldDelegate>

Then you set MyObject as the delegate of the NSTextField

[myTextField setDelegate:myObject]

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

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