Implementation of a char-by-char text field's verification

I try to implement a continuous verification of a text field: by typing a integer ID, the user sees if the ID is not yet attributed (red message near the text field and “OK” button disabled). This:

gProposedCardIDWarning's setHidden_(not gNavigatorArray's containsObject_(gProposedCardID))

works well but too late: if the user types, say, 12 and then clicks on the OK button, the control is bypassed.

Any hints? Thanks

Hi,

implement the delegate notification controlTextDidChange_(aNotification) in your controller class and connect the delegate of the text field to the controller class in Interface Builder

Thank you stefan.

controlTextDidChange_ leads to four pages of Objective-C in the documentation, my eyes are blushing… It sounds simple (and certainly is) but these concepts are new to me…

“implement the delegate notification” = create a handler in the code?
“in your controller class” = do I have to create a new class, with interface/implementation files?
“the delegate of the text field” is a delegation by right-clicking the objects in IB?

Thanks for your patience ” of course I should have tried something less ambitious, like “if theNumber is in myList then display alert”, but I learn more when it’s difficult…

Yes

the class where you’re going to implement the handler, usually the default AppDelegate class

control-click the text field and drag it to the “controller class” icon, choose delegate from the shown action menu

Let’s see if there is a tiny candle in my darkness: to “delegate” means just to “pass notifications”?:confused:

Yes, delegates are also used for protocol methods, which are technically no notifications

I end with a

NSDocumentController Info.plist warning: The values of CFBundleTypeRole entries must be ‘Editor’, ‘Viewer’, ‘None’, or ‘Shell’.

I goofed again.

I’ve just tested it successfully with a document based ASOC project, this handler is put into MyDocument.applescript


on controlTextDidChange_(aNotification)
	log aNotification's object()'s stringValue()
end controlTextDidChange_

and the delegate of the text field is connected to the File’s Owner cube

No this was just another bug…

My “Send Actions” on the IB binding panel shows “controlTextDidChange” linked to “MyApp Delegate”.

But there is no notification sent to my handler:

    on controlTextDidChange_(aNotification)
        log "Changed"
    end controlTextDidChange_

Nothing is output on the log console…

It’s not an action, it’s the delegate, don’t define a action property

It’s OK, it works muchas gracias! :smiley:

But… I loose the effect of my Number formatter (on the same field) - the user can type anything, including alphabetics, negative or huge numbers… the formatter appears greyed out. Is there only one possible delegate for notifications?

In the controlTextDidChange_(aNotification) method you are able to filter the characters programmatically

Of course I should be able to do that, but

NSConcreteNotification 0x1263200 {name = NSControlTextDidChangeNotification; object = <NSTextField: 0x121ed50>; userInfo = {
NSFieldEditor = “<NSTextView: 0x14595d0>\n Frame = {{2.00, 3.00}, {92.00, 16.00}}, Bounds = {{0.00, 0.00}, {92.00, 16.00}}\n Horizontally resizable: YES, Vertically resizable: YES\n MinSize = {92.00, 16.00}, MaxSize = {40000.00, 40000.00}\n”;
}}

is definitely not so handy as an old eventRecord… what can I make with this? For instance, how recognize which character was entered ? I must test the entire contents of the text field (as string)?

Get the integer value of the text field (the object of the notification)
If the string is not a valid integer, the result will be 0.
In this case get the string value of the text field, cut the last character and write the string back into the text field.

For the user it seems that the key was not accepted

Finally solved so (the error stay displayed so the user knows what’s wrong) :

    on controlTextDidChange_(aNotification)
        set acceptable to (gProposedCardID is not missing value) and (gProposedCardID as integer > 1) and (gProposedCardID as integer < 32767) and (not gNavigatorArray's containsObject_(gProposedCardID))
        gProposedCardIDWarning's setHidden_(acceptable)
        gCreateWindowOKButton's setEnabled_(acceptable)
    end controlTextDidChange_

looks finally close enough to Pascal :wink:

Good night!