macos - Obtenga valor de NSTextField
cocoa (4)
Tengo un NSTextField y necesito obtener el valor del campo en una variable. ¿Cuál es el método apropiado?
Para un NSString usaría:
NSString *myString = [theTextField stringValue];
Para una int deberías usar:
int myInt = [theTextField intValue];
Hay muchos otros métodos para obtener el valor de un control. Eche un vistazo a la referencia de NSControl para obtener más información, en la sección "Obtener y establecer el valor del control" .
Aquí hay una lista:
-
doubleValue -
floatValue -
intValue -
integerValue -
objectValue -
stringValue -
attributedStringValue
También:
Supongamos que tiene un objeto ( MyObject ) que quiere recibir una notificación cuando alguien escribe en un NSTextField . En el archivo .h, MyObject debe declarar que se ajusta a NSTextFieldDelegate , como en ...
@interface MyObject : NSObject <NSTextFieldDelegate>
Luego configura MyObject como el delegado de NSTextField
[myTextField setDelegate:myObject]
Ahora, puedes descubrir cuándo sucede algo en el campo de texto implementando métodos en MyObject como:
-(void)controlTextDidEndEditing:(NSNotification *)aNotification;
-(void)controlTextDidChange:(NSNotification *)aNotification;
-(void)controlTextDidBeginEditing:(NSNotification *)aNotification;
Versión de Swift 3.x:
myField.stringValue
[myField stringValue]
NSTextField hereda de NSControl , y NSControl define los stringValue / setStringvalue: methods.