Important: The information in this document is obsolete and should not be used for new development.
| Inherits from | |
| Implements | |
| Package | com.apple.cocoa.application |
| Companion guide |
NSControl is an abstract superclass that provides three fundamental features for implementing user interface devices: drawing devices on the screen, responding to user events, and sending action messages. It works closely with NSCell.
NSControl provides several delegate methods for its subclasses that allow text editing, such as NSTextField and NSMatrix. Note that although NSControl defines delegate methods, it does not itself have a delegate. Any subclass that uses these methods must have a delegate and the methods to get and set it.
Note that although NSControl defines delegate methods, it does not itself have a delegate. Any subclass that uses these methods must have a delegate and the methods to get and set it.
doubleValue
setDoubleValue
floatValue
setFloatValue
intValue
setIntValue
objectValue
setObjectValue
stringValue
setStringValue
setNeedsDisplay
attributedStringValue
setAttributedStringValue
action
setAction
target
setTarget
isContinuous
setContinuous
sendActionToTarget
setEventMaskForSendingAction
controlIsValidObject delegate method
controlTextShouldBeginEditing delegate method
controlTextShouldEndEditing delegate method
controlTextDidBeginEditing delegate method
controlTextDidChange delegate method
controlTextDidEndEditing delegate method
controlDidFailToFormatStringErrorDescription delegate method
controlDidFailToValidatePartialString delegate method
controlTextViewDoCommandBySelector delegate method
controlTextViewCompletionsForPartialWordRange delegate method
Creates an NSControl object with a zero-sized frame rectangle and creates a cell for it if the cell’s class has been specified for controls of this type with setCellClass.
public NSControl()
Because NSControl is an abstract class, you should call this constructor only in the constructors of subclasses; that is, there should always be a more specific constructor for the subclass, as this is the constructor for NSControl.
Creates an NSControl object in frameRect and creates a cell for it if the cell’s class has been specified for controls of this type with setCellClass.
public NSControl(NSRect frameRect)
Because NSControl is an abstract class, you should call this constructor only in the constructors of subclasses; that is, there should always be a more specific constructor for the subclass, as this is the constructor for NSControl.
Returns the class of cells used by the receiving class.
public static Class cellClass()
Returns null if no cell class has been specified for the receiving class or any of its superclasses (up to NSControl).
Sets the class of cells used by instances of the receiver.
public static void setCellClass(Class class)
Terminates and discards any editing of text displayed by the receiver and removes the field editor’s delegate.
public boolean abortEditing()
Returns true if there was a field editor associated with the control, false otherwise.
Returns the action-message selector of the receiver’s cell (the default NSControl behavior), or the default action-message selector for a control with multiple cells (such as an NSMatrix or an NSForm).
public NSSelector action()
For controls with multiple cells, it’s better to get the action-message selector for a particular cell.
Returns the alignment mode of the text in the receiver’s cell.
public int alignment()
The return value can be one of these constants: NSText.LeftTextAlignment, NSText.RightTextAlignment, NSText.CenterTextAlignment, NSText.JustifiedTextAlignment, or NSText.NaturalTextAlignment (the default alignment).
Returns the object value of the receiver’s cell (or selected cell) as an attributed string after validating any editing currently being done.
public NSAttributedString attributedStringValue()
If no cell is associated with the receiver, returns an empty attributed string.
Recomputes any internal sizing information for the receiver, if necessary, by invoking its NSCell’s calcDrawInfo method.
public void calcSize()
Most NSControls maintain a flag that informs them if any of their cells have been modified in such a way that the location or size of the cell should be recomputed. If such a modification happens, calcSize is automatically invoked whenever the NSControl is displayed; you never need to invoke it yourself.
Returns the receiver’s cell.
public NSCell cell()
In NSControls with multiple cells (such as NSMatrix or NSForm), use selectedCell or a similar method for finding a particular cell.
public NSText currentEditor()
If the receiver is being edited—that is, it has a field editor and is the first responder of its NSWindow—this method returns the field editor; otherwise, it returns null.
The field editor is a single NSTextView object that is shared among all the controls in a window for light text-editing needs. It is automatically instantiated when needed.
Returns the value of the receiver’s cell as a double-precision floating-point number.
public double doubleValue()
If the NSControl contains many cells (for example, NSMatrix), then the value of the currently selected cell is returned. If the NSControl is in the process of editing the affected cell, then it invokes validateEditing before extracting and returning the value.
If aCell is the cell used to implement the receiver, then the receiver is displayed.
public void drawCell(NSCell aCell)
This method is provided primarily to support a consistent set of methods between NSControls with single and multiple cells, because an NSControl with multiple cells needs to be able to draw a single cell at a time.
Draws the inside of the receiver’s cell (the area within a bezel or border) specified by aCell.
public void drawCellInside(NSCell aCell)
If the receiver is transparent, the method causes the superview to draw itself. This method invokes NSCell’s drawInteriorWithFrameInView method. This method has no effect on NSControls (such as NSMatrix and NSForm) that have multiple cells.
Returns the value of the receiver’s cell (or selected cell, if a multiple-cell NSControl) as a single-precision floating-point number.
public float floatValue()
See doubleValue for more details.
Returns the NSFont used to draw text in the receiver’s cell.
public NSFont font()
Returns the receiver’s formatter.
public Object formatter()
Returns whether the receiver ignores multiple clicks made in rapid succession.
public boolean ignoresMultiClick()
See setIgnoresMultiClick for details.
Returns the value of the receiver’s cell (or selected cell, if a multiple-cell NSControl) as an integer.
public int intValue()
See doubleValue for more details.
Returns whether the receiver’s NSCell continuously sends its action message to its target during mouse tracking.
public boolean isContinuous()
Returns whether the receiver reacts to mouse events.
public boolean isEnabled()
public void mouseDown(NSEvent theEvent)
Invoked when the mouse button is pressed while the cursor is within the bounds of the receiver, generating theEvent. This method highlights the receiver’s NSCell and sends it a trackMouse message. Whenever the NSCell finishes tracking the mouse (for example, because the cursor has left the cell’s bounds), the cell is unhighlighted. If the mouse button is still down and the cursor reenters the bounds, the cell is again highlighted and a new trackMouse message is sent. This behavior repeats until the mouse button goes up. If it goes up with the cursor in the control, the state of the control is changed, and the action message is sent to the target. If the mouse button goes up when the cursor is outside the control, no action message is sent.
ignoresMultiClicktrackMouse (NSCell)Returns the value of the receiver’s cell (or selected cell, if a multiple-cell NSControl) as an object.
public Object objectValue()
See doubleValue for more details.
Can be used to simulate a single mouse click on the receiver.
public void performClick(Object sender)
sender is ignored. This method calls performClick on the receiver's cell with the sender being the control itself. Throws an exception if the action message cannot be successfully sent.
Returns whether the receiver refuses first responder status.
public boolean refusesFirstResponder()
Selects aCell (by setting its state to true) and redraws the NSControl if aCell is a cell of the receiver and is unselected.
public void selectCell(NSCell aCell)
Returns the receiver’s selected cell.
public NSCell selectedCell()
The default implementation for NSControl simply returns the associated cell (or null if no cell has been set). Subclasses of NSControl that manage multiple cells (such as NSMatrix and NSForm) override this method to return the cell selected by users.
Returns the tag integer of the receiver’s selected cell (see selectedCell) or –1 if there is no selected cell.
public int selectedTag()
When you set the tag of a control with a single cell in Interface Builder, it sets the tags of both the control and the cell with the same value as a convenience.
Sends sendActionToTargetFromSender to NSApplication.sharedApplication(), which in turn sends a message to theTarget to perform theAction, adding the receiver as the last parameter.
public boolean sendActionToTarget(NSSelector theAction, Object theTarget)
sendActionToTarget is invoked primarily by NSCell’s trackMouse.
If theAction is NULL, no message is sent. If theTarget is null, NSApplication.sharedApplication() looks for an object that can respond to the message by following the responder chain (see the class description for NSActionCell). This method returns false if no object that responds to theAction could be found.
Sets the receiver’s action method to aSelector.
public void setAction(NSSelector aSelector)
If aSelector is NULL, then no action messages will be sent from the receiver.
See Action Messages for additional information on action messages.
Sets the alignment of text in the receiver’s cell and, if the cell is being edited, aborts editing and updates the cell.
public void setAlignment(int mode)
mode is one of five constants: NSText.LeftTextAlignment, NSText.RightTextAlignment, NSText.CenterTextAlignment, NSText.JustifiedTextAlignment, NSText.NaturalTextAlignment (the default alignment for the text).
Sets the value of the receiver’s cell (or selected cell) as an attributed string object.
public void setAttributedStringValue(NSAttributedString object)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks it for automatic redisplay (NSActionCell performs its own updating of cells).
Sets the receiver’s cell to aCell.
public void setCell(NSCell aCell)
Use this method with great care as it can irrevocably damage the affected control; specifically, you should only use this method in initializers for subclasses of NSControl.
Sets whether the receiver’s cell continuously sends its action message to its target as it tracks the mouse, depending on the Boolean value flag.
public void setContinuous(boolean flag)
Sets the value of the receiver’s cell (or selected cell) to aDouble (a double-precision floating-point number).
public void setDoubleValue(double aDouble)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks the cell’s interior as needing to be redisplayed (NSActionCell performs its own updating of cells).
Sets whether the receiver’s cell—or if there is no associated cell, the NSControl itself—is active (that is, whether it tracks the mouse and sends its action to its target).
public void setEnabled(boolean flag)
If flag is false, any editing is aborted. Redraws the entire control if it is marked as needing redisplay. Subclasses may want to override this method to redraw only a portion of the control when the enabled state changes, as do NSButton and NSSlider.
Sets the conditions on which the receiver sends action messages to its target and returns a bit mask with which to detect the previous settings.
public int setEventMaskForSendingAction(int mask)
You use this method during mouse tracking when the mouse button changes state, the mouse moves, or—if the cell is marked to send its action continuously while tracking—periodically. Because of this, the only bits checked in mask are LeftMouseDownMask, LeftMouseUpMask, LeftMouseDraggedMask, and PeriodicMask, which are declared in NSEvent.h. NSControl’s default implementation simply invokes the setEventMaskForSendingAction method of its associated cell.
Sets the autoranging and floating point number format of the receiver’s cell, so that at most leftDigits are displayed to the left of the decimal point, and rightDigits to the right.
public void setFloatingPointFormat(boolean autoRange, int leftDigits, int rightDigits)
The autoRange argument specifies whether floating-point numbers are autoranged in the receiver. See the description of this method in the NSCell class specification for details. If the cell is being edited, what’s typed is discarded, and the cell’s interior is redrawn.
setFloatingPointFormat (NSCell)Sets the value of the receiver’s cell (or selected cell) to aFloat (a single-precision floating-point number).
public void setFloatValue(float aFloat)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks the cell’s interior as needing to be redisplayed (NSActionCell performs its own updating of cells).
Sets the font used to draw text in the receiver’s cell to fontObject.
public void setFont(NSFont fontObject)
If the cell is being edited, the text in the cell is redrawn in the new font, and the cell’s editor (the NSText object used globally for editing) is updated with the new NSFont.
Sets the receiver’s formatter to newFormatter.
public void setFormatter(NSFormatter newFormatter)
Sets whether the receiver ignores multiple clicks made in rapid succession, depending on the Boolean value flag.
public void setIgnoresMultiClick(boolean flag)
By default, controls treat double clicks as two distinct clicks, triple clicks as three distinct clicks, and so on. However, when an NSControl returning true to this method receives multiple clicks (within a predetermined interval), each mouseDown event after the first is passed on to super.
Sets the value of the receiver’s cell (or selected cell) to the integer anInt.
public void setIntValue(int anInt)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks the cell’s interior as needing to be redisplayed (NSActionCell performs its own updating of cells).
Marks the receiver as needing redisplay (assuming automatic display is enabled) after recalculation of its dimensions.
public void setNeedsDisplay()
setNeedsDisplay (NSView)Sets the value of the receiver’s cell (or selected cell) to object.
public void setObjectValue(Object object)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks the cell’s interior as needing to be redisplayed (NSActionCell performs its own updating of cells).
Sets whether the receiver refuses first responder status, depending on the Boolean value flag.
public void setRefusesFirstResponder(boolean flag)
By default, the user can advance the focus of keyboard events between controls by pressing the Tab key; when this focus—or first responder status—is indicated for a control (by the insertion point or, for nontext controls, a faint rectangle), the user can activate the control by pressing the Space bar.
Sets the value of the receiver’s cell (or selected cell) to the string aString.
public void setStringValue(String aString)
If the cell is being edited, it aborts all editing before setting the value; if the cell doesn’t inherit from NSActionCell, it marks the cell’s interior as needing to be redisplayed (NSActionCell performs its own updating of cells).
Sets the tag of the receiver to anInt.
public void setTag(int anInt)
It doesn’t affect the tag of the receiver’s cell. Tags allow you to identify particular cells. Tag values are not used internally; they are only changed by external invocations of setTag. You typically set tag values in Interface Builder. When you set the tag of a control with a single cell in Interface Builder, it sets the tags of both the control and the cell to the same value as a convenience.
Sets the target object for the action message of the receiver’s cell; NSCell’s setTarget is used instead of any subclass override of this method.
public void setTarget(Object anObject)
If anObject is null and the control sends an action message, the application looks for an object that can respond to the message by following the responder chain (see description of the NSActionCell class for details).
Changes the width and the height of the receiver’s frame so they are the minimum needed to contain its cell.
public void sizeToFit()
If you want a multiple-cell custom subclass of NSControl to size itself to fit its cells, you must override this method. This method neither redisplays the receiver nor marks it as needing display. You must do this yourself with either display or setNeedsDisplay.
Returns the value of the receiver’s cell (or selected cell, if a multiple-cell NSControl) as a String.
public String stringValue()
See doubleValue for details.
Returns the tag identifying the receiver (not the tag of the receiver’s cell).
public int tag()
Tags allow you to identify particular controls. Tag values are not used internally; they are only changed by external invocations of setTag. You typically set tag values in Interface Builder. When you set the tag of a control with a single cell in Interface Builder, it sets the tags of both the control and the cell to the same value as a convenience.
Sets the double-precision floating-point value of the receiver’s cell (or selected cell) to the value obtained by sending a doubleValue message to sender.
public void takeDoubleValue(Object sender)
You can use this method to link action messages between controls. It permits one control or cell (sender) to affect the value of another control (the receiver) by invoking this method in an action message to the receiver. For example, a text field can be made the target of a slider. Whenever the slider is moved, it will send a takeDoubleValue message to the text field. The text field will then get the slider’s floating-point value, turn it into a text string, and display it, thus tracking the value of the slider.
Sets the receiver’s selected cell to the value obtained by sending a floatValue message to sender.
public void takeFloatValue(Object sender)
See takeDoubleValue for more information.
Sets the receiver’s selected cell to the value obtained by sending an intValue message to sender.
public void takeIntValue(Object sender)
See takeDoubleValue for more information.
Sets the receiver’s selected cell to the value obtained by sending an objectValue message to sender.
public void takeObjectValue(Object sender)
See takeDoubleValue for more information.
Sets the receiver’s selected cell to the value obtained by sending a stringValue message to sender.
public void takeStringValue(Object sender)
See takeDoubleValue for more information.
Returns the target object of the receiver’s cell.
public Object target()
Redisplays aCell or marks it for redisplay.
public void updateCell(NSCell aCell)
Redisplays the inside of aCell or marks it for redisplay.
public void updateCellInside(NSCell aCell)
Validates the user’s changes to text in a cell of the receiving control.
public void validateEditing()
Validation sets the object value of the cell to the current contents of the cell’s editor (the NSText object used for editing), storing it as a simple String or an attributed string object based on the attributes of the editor.
Invoked when the formatter for the cell belonging to control (or selected cell) cannot convert a String to an underlying object.
public abstract boolean controlDidFailToFormatStringErrorDescription(NSControl control, String string, String error)
error is a localized user-presentable String that explains why the conversion failed. Evaluate the error or query the user and return true if string should be accepted as is, or false if string should be rejected.
objectValueForString (NSFormatter)Invoked when the formatter for the cell belonging to control (or selected cell) rejects a partial string a user is typing into the cell.
public abstract void controlDidFailToValidatePartialString(NSControl control, String string, String error)
This String (string) includes the character that caused the rejection. error is a localized user-presentable String that explains why the validation failed. You can implement this method to display a warning message or perform a similar action when the user enters improperly formatted text.
isPartialStringValid (NSFormatter)Invoked when the insertion point leaves a cell belonging to control, but before the string value of the cell’s object is displayed.
public abstract boolean controlIsValidObject(NSControl control, Object object)
Return true to allow display of the string and false to reject display and return the cursor to the cell. This method gives the delegate the opportunity to validate the contents of the control’s cell (or selected cell). In validating, the delegate checks object to determine if it falls within a permissible range, has required attributes, accords with a given context, and so on. Examples of objects subject to such evaluations are an NSDate object that should not represent a future date or a monetary amount (represented by an NSNumber) that exceeds a predetermined limit.
Sent by the default notification center to the delegate and all observers of the notification when a control with editable cells (such as a text field, a form field, or an NSMatrix) begins editing text.
public abstract void controlTextDidBeginEditing(NSNotification aNotification)
The name of notification aNotification is always ControlTextDidBeginEditingNotification. Use the key "NSFieldEditor" to obtain the field editor from aNotification’s userInfo dictionary. If the delegate implements this method, it’s automatically registered to receive this notification.
Sent by the default notification center to the delegate when the text in the receiving control (usually a text field, a form field, or NSMatrix with editable cells) changes.
public abstract void controlTextDidChange(NSNotification aNotification)
The name of notification aNotification is always ControlTextDidChangeNotification. Use the key "NSFieldEditor" to obtain the field editor from aNotification’s userInfo dictionary. If the delegate implements this method, it’s automatically registered to receive this notification.
Sent by the default notification center to the delegate and all observers of the notification when a control with editable cells (such as a text field, a form field, or an NSMatrix) ends editing text.
public abstract void controlTextDidEndEditing(NSNotification aNotification)
The name of notification aNotification is always ControlTextDidEndEditingNotification. Use the key "NSFieldEditor" to obtain the field editor from aNotification’s userInfo dictionary. If the delegate implements this method, it’s automatically registered to receive this notification.
Sent directly by control to the delegate when the user tries to enter a character in a cell of a control that allows editing of text (such as a text field or form field).
public abstract boolean controlTextShouldBeginEditing(NSControl control, NSText fieldEditor)
Return true if the NSControl’s fieldEditor should be allowed to start editing the text, false otherwise.
Sent directly by control to the delegate when the insertion point tries to leave a cell of the control that has been edited.
public abstract boolean controlTextShouldEndEditing(NSControl control, NSText fieldEditor)
It’s sent only by controls that allow editing of text (such as a text field or a form field). Return true if the control’s fieldEditor should be allowed to end its edit session, false otherwise.
Sent to the delegate to allow you to control the list of proposed text completions generated by text fields and other controls.
public abstract NSArray controlTextViewCompletionsForPartialWordRange(NSControl control, NSTextView textView, NSArray words, NSRange charRange)
Arguments include the text view for which the list was generated, the list of potential completions, and the partial word range (that is, the number of characters the user has already typed). The array you return is used for the completions list in place of the potential list passed to this method.
The completions list is an array of strings, in the order in which they should be presented. Each string should be a complete word that the user might be trying to type. The strings must be complete words rather than just the remainder of the word, in case completion requires some slight modification of what the user has already typed—for example, the addition of an accent, or a change in capitalization. You can also use this method to support abbreviations that complete into words that don't start with the characters of the abbreviation.
Invoked when users press keys with predefined bindings in a cell of the control or selected cell, as communicated to the control by the cell’s field editor (textView).
public abstract boolean controlTextViewDoCommandBySelector(NSControl control, NSTextView textView, NSSelector command)
The delegate returns true if it handles the key binding, and false otherwise. These bindings are usually implemented as methods (command) defined in NSResponder; examples of such key bindings are arrow keys (for directional movement) and the Escape key (for name completion). By implementing this method, the delegate can override the default implementation of command and supply its own behavior.
NSControl posts the following notifications to interested observers and its delegate.
The field editor of the edited cell originally sends a TextDidBeginEditingNotification to the control, which passes it on in this form to its delegate. The notification object is the NSControl posting the notification. The userInfo dictionary contains the following information:
Key |
Value |
|---|---|
|
The edited cell’s field editor |
See the description of controlTextDidBeginEditing for details.
The field editor of the edited cell originally sends a TextDidChangeNotification to the control, which passes it on in this form to its delegate. The notification object is the NSControl posting the notification. The userInfo dictionary contains the following information:
Key |
Value |
|---|---|
|
The edited cell’s field editor |
See the description of controlTextDidChange for details.
The field editor of the edited cell originally sends a ControlTextDidEndEditingNotification to the control, which passes it on in this form to its delegate. The notification object is the NSControl posting the notification. The userInfo dictionary contains the following information:
Key |
Value |
|---|---|
|
The edited cell’s field editor |
See the description of controlTextDidEndEditing for details.
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)