Hello,
I want to build a control that is similar to the NSDatePicker. It´s cells should validate in the same way and it´s stepper should change the value of the cell that is in focus. I want to use the control for date/time offset values.
For a start, I embedded a few NSTextFields and a stepper into a NSView and I added custom NumberFormatters to the TextFields. This, at least, looks similar to the DatePicker.
But I need to validate the input immidiately to contrain the input to numbers with two digits only and for the stepper to apply it to the focused TextField. But how do I find the selected/focused control?
THX
Browser: Firefox 11.0
Operating System: Mac OS X (10.6)
If I understand your question correctly, then what you are probably looking for is currentEditor from the NSControl class. You call it like this:
if ([myTextView currentEditor] != nil) { //is active NSControl }
this is cocoa. The ASOC equivalent would be:
if (myTextView's currentEditor) ≠missing value then --do something
This is of course assuming that myTextView is a binding to an NSTextView object in your NIB file, which is an NSControl object.
Hope this helps!
Model: MacBookPro8,2
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)
hi thanks, it does not work yet. My outlet refers to an NSTextField, not NSTextView, may be there is a difference. I get the error message:
currentEditor of «class ocid» id «data kptr000000006016C60002000000» kann nicht in Typ reference umgewandelt werden. (error -1700)
sorry, it´s German: current editor of class … can´t be converted to type reference.
In the meanwhile I was thinking about firstResponder. I tried
set myObj to myView's firstResponder
– myView being either an outlet connected to the nib´s view or to another NSView object that hosts the TextFields.
aahahh - just got it::
if (myTextView's currentEditor()) ≠missing value then --do something
Note the empty brackets: currentEditor()
Oh, yes! Sorry, forgot the brackets… been awhile since i’ve used ASOC.
I was surprised it didnt work, because NSTextField is a subclass of NSControl.
The firstResponder method only send you the object in your view which is set to be the first responder, not which one is being edited. This is where currentEditor comes handy.
So problem solved I guess?
This one is!
The other part of the question was about how to immidiately validate the input. The textDidChange notification looks promising. How do I use it?
There is a couple of things. First one is using a delegate for your text view and use controlTextDidChange from NScontrol. There is other delegate methods in NScontrol, one of which responds to each and every key the user types.
There is another method which I don’t remember at the moment that lets you validate the entry and prevent the user from escaping the control until the problem is fixed. I have to get back to my computer before I can find that info though.
I just startet my second attempt to understand ASOC (I gave up the first attepmtwo years ago). So I do not yet understand the concept of a delegate and how to use it.
There is a “delegate” item in the connections panel. What do I link it to?
Please excuse the low knowledge level of my question.
A delegate is a sort of helper object. Instead of modifying the control itself, you set some other class (probably your app delegate, which is already the delegate of your app) to be its delegate. This means when certain things happen to the object, it notifies its delegate by calling the delegate methods.
Thank you for this short but precise explanation. I connected the TextField’s delegate to File’s Owner and added a handler to my app:
on controlTextDidChange_(sender)
log "controlTextDidChange_"
end controlTextDidChange_
The handler is called every time the TF is changed - great!