UIControl Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/UIKit.framework |
| Availability | Available in iOS 2.0 and later. |
| Declared in | UIControl.h |
Overview
UIControl is the base class for control objects such as buttons and sliders that convey user intent to the application. You cannot use the UIControl class directly to instantiate controls. It instead defines the common interface and behavioral structure for all its subclasses.
The main role of UIControl is to define an interface and base implementation for preparing action messages and initially dispatching them to their targets when certain events occur.
For an overview of the target-action mechanism, see “Target-Action in UIKit” in Cocoa Fundamentals Guide. For information on the Multi-Touch event model, see Event Handling Guide for iOS.
The UIControl class also includes methods for getting and setting control state—for example, for determining whether a control is enabled or highlighted—and it defines methods for tracking touches within a control. These tracking methods are overridden by UIControl subclasses.
Subclassing Notes
You may want to extend a UIControl subclass for either of two reasons:
To observe or modify the dispatch of action messages to targets for particular events
To do this, override
sendAction:to:forEvent:, evaluate the passed-in selector, target object, orUIControlEventsbit mask, and proceed as required.To provide custom tracking behavior (for example, to change the highlight appearance)
To do this, override one or all of the following methods:
beginTrackingWithTouch:withEvent:,continueTrackingWithTouch:withEvent:,endTrackingWithTouch:withEvent:.
Tasks
Preparing and Sending Action Messages
-
– sendAction:to:forEvent: -
– sendActionsForControlEvents: -
– addTarget:action:forControlEvents: -
– removeTarget:action:forControlEvents: -
– actionsForTarget:forControlEvent: -
– allTargets -
– allControlEvents
Setting and Getting Control Attributes
-
stateproperty -
enabledproperty -
selectedproperty -
highlightedproperty -
contentVerticalAlignmentproperty -
contentHorizontalAlignmentproperty
Tracking Touches and Redrawing Controls
Properties
contentHorizontalAlignment
The horizontal alignment of content (text or image) within the receiver.
Parameters
- contentAlignment
A constant that specifies the alignment of text or image within the receiver. See “Horizontal Content Alignment” for descriptions of valid constants.
Discussion
The value of this property is a constant that specifies the alignment of text or image within the receiver. The default is UIControlContentHorizontalAlignmentLeft.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hcontentVerticalAlignment
The vertical alignment of content (text or image) within the receiver.
Parameters
- contentAlignment
A constant that specifies the alignment of text or image within the receiver. See “Vertical Content Alignment” for descriptions of valid constants.
Discussion
This value of this property is a constant that specifies the alignment of text or image within the receiver. The default is UIControlContentVerticalAlignmentTop.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.henabled
A Boolean value that determines whether the receiver is enabled.
Discussion
Specify YES to make the control enabled; otherwise, specify NO to make it disabled. The default value is YES. If the enabled state is NO, the control ignores touch events and subclasses may draw differently.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hhighlighted
A Boolean value that determines whether the receiver is highlighted.
Discussion
Specify YES if the control is highlighted; otherwise NO. By default, a control is not highlighted. UIControl automatically sets and clears this state automatically when a touch enters and exits during tracking and when there is a touch up.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hselected
A Boolean value that determines the receiver’s selected state.
Discussion
Specify YES if the control is selected; otherwise NO. The default is NO. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, UISwitchControl) or the application object might read or set this control state.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hstate
A Boolean value that indicates the state of the receiver. (read-only)
Discussion
One or more UIControlState bit-mask constants that specify the state of the UIControl object; for information on these constants, see “Control State.” Note that the control can be in more than one state, for example, both disabled and selected (UIControlStateDisabled | UIControlStateSelected).This attribute is read only—there is no corresponding setter method.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.htouchInside
A Boolean value that indicates whether a touch is inside the bounds of the receiver. (read-only)
Return Value
YES if a touch is inside the receiver’s bounds; otherwise NO.
Discussion
The value is YES if a touch is inside the receiver’s bounds; otherwise the value is NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.htracking
A Boolean value that indicates whether the receiver is currently tracking touches related to an event. (read-only)
Discussion
The value is YES if the receiver is tracking touches; otherwiseNO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hInstance Methods
actionsForTarget:forControlEvent:
Returns the actions that are associated with a target and a particular control event.
Parameters
- target
The target object—that is, the object to which an action message is sent.
You must pass an explicit match for the target. Do not pass a value of
nil.- controlEvent
A single constant of type
UIControlEventsthat specifies a particular user action on the control; for a list of these constants, see “Control Events.”
Return Value
An array of selector names as NSString objects or nil if there are no action selectors associated with the control event.
Discussion
Pass in a selector name to the NSSelectorFromString function to obtain the selector (SEL) value.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.haddTarget:action:forControlEvents:
Adds a target and action for a particular event (or events) to an internal dispatch table.
Parameters
- target
The target object—that is, the object to which the action message is sent. If this is
nil, the responder chain is searched for an object willing to respond to the action message.- action
A selector identifying an action message. It cannot be
NULL.- controlEvents
A bitmask specifying the control events for which the action message is sent. See “Control Events” for bitmask constants.
Discussion
You may call this method multiple times, and you may specify multiple target-action pairs for a particular event. The action message may optionally include the sender and the event as parameters, in that order.
When you call this method, target is not retained.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hallControlEvents
Returns all control events associated with the receiver.
Return Value
One or more UIControlEvents constants that specify the current control events associated with the receiver; for a list of these constants, see “Control Events”list of all events that have at least one action.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hallTargets
Returns all target objects associated with the receiver.
Return Value
A set of all targets—that is, the objects to which action messages are sent—for the receiver. The set may include NSNull to indicate at least one nil target (meaning, the responder chain is searched for a target).
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hbeginTrackingWithTouch:withEvent:
Sent to the control when a touch related to the given event enters the control’s bounds.
Parameters
- touch
A
UITouchobject that represents a touch on the receiving control during tracking.- event
An event object encapsulating the information specific to the user event.
Return Value
YES if the receiver is set to respond continuously or set to respond when a touch is dragged; otherwise NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hcancelTrackingWithEvent:
Tells the control to cancel tracking related to the given event.
Parameters
- event
An event object encapsulating the information specific to the user event. This parameter might be
nil, indicating that the cancelation was caused by something other than an event, such as the view being removed from the window.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hcontinueTrackingWithTouch:withEvent:
Sent continuously to the control as it tracks a touch related to the given event within the control’s bounds.
Parameters
- touch
A
UITouchobject that represents a touch on the receiving control during tracking.- event
An event object encapsulating the information specific to the user event
Return Value
YES if touch tracking should continue; otherwise NO.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hendTrackingWithTouch:withEvent:
Sent to the control when the last touch for the given event completely ends, telling it to stop tracking.
Parameters
- touches
A
UITouchobject that represents a touch on the receiving control during tracking.- event
An event object encapsulating the information specific to the user event.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hremoveTarget:action:forControlEvents:
Removes a target and action for a particular event (or events) from an internal dispatch table.
Parameters
- target
The target object—that is, the object to which the action message is sent. Pass
nilto remove all targets paired with action and the specified control events.- action
A selector identifying an action message. Pass
NULLto remove all action messages paired with target.- controlEvents
A bitmask specifying the control events associated with target and action. See “Control Events” for bitmask constants.
Availability
- Available in iOS 2.0 and later.
See Also
Declared In
UIControl.hsendAction:to:forEvent:
In response to a given event, forwards an action message to the application object for dispatching to a target.
Parameters
- action
A selector identifying an action message. It cannot be
NULL.- target
The target object—that is, the object to which the action message is sent. If this is
nil, the receiver traverses the responder chain and sends the action message to the first object willing to respond to it.- event
An object representing the event (typically in a
UIControlobject) that originated the action message. The event can benilif the action is invoked directly instead of being caused by an event. For example, a value-changed message might be sent for programmatic reasons rather than as a result of the user touching the control.
Discussion
UIControl implements this method to forward an action message to the singleton UIApplication object (in its sendAction:to:fromSender:forEvent: method) for dispatching it to the target or, if there is no specified target, to the first object in the responder chain that is willing to handle it. Subclasses may override this method to observe or modify action-forwarding behavior. The implementation of sendActionsForControlEvents: might call this method repeatedly, once for each specified control event.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hsendActionsForControlEvents:
Sends action messages for the given control events.
Parameters
- controlEvents
A bitmask whose set flags specify the control events for which action messages are sent. See “Control Events” for bitmask constants.
Discussion
UIControl implements this method to send all action messages associated with controlEvents, repeatedly invoking sendAction:to:forEvent: in the process. The list of targets and actions it looks up is constructed from prior invocations of addTarget:action:forControlEvents:.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hConstants
UIControlEvents
The type for the enum constants listed in “Control Events.”
typedef NSUInteger UIControlEvents;
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.hControl Events
Kinds of events possible for control objects.
enum {
UIControlEventTouchDown = 1 << 0,
UIControlEventTouchDownRepeat = 1 << 1,
UIControlEventTouchDragInside = 1 << 2,
UIControlEventTouchDragOutside = 1 << 3,
UIControlEventTouchDragEnter = 1 << 4,
UIControlEventTouchDragExit = 1 << 5,
UIControlEventTouchUpInside = 1 << 6,
UIControlEventTouchUpOutside = 1 << 7,
UIControlEventTouchCancel = 1 << 8,
UIControlEventValueChanged = 1 << 12,
UIControlEventEditingDidBegin = 1 << 16,
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19,
UIControlEventAllTouchEvents = 0x00000FFF,
UIControlEventAllEditingEvents = 0x000F0000,
UIControlEventApplicationReserved = 0x0F000000,
UIControlEventSystemReserved = 0xF0000000,
UIControlEventAllEvents = 0xFFFFFFFF
};
Constants
UIControlEventTouchDownA touch-down event in the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchDownRepeatA repeated touch-down event in the control; for this event the value of the UITouch
tapCountmethod is greater than one.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchDragInsideAn event where a finger is dragged inside the bounds of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchDragOutsideAn event where a finger is dragged just outside the bounds of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchDragEnterAn event where a finger is dragged into the bounds of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchDragExitAn event where a finger is dragged from within a control to outside its bounds.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchUpInsideA touch-up event in the control where the finger is inside the bounds of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchUpOutsideA touch-up event in the control where the finger is outside the bounds of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventTouchCancelA system event canceling the current touches for the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventValueChangedA touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventEditingDidBeginA touch initiating an editing session in a
UITextFieldobject by entering its bounds.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventEditingChangedA touch making an editing change in a
UITextFieldobjet.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventEditingDidEndA touch ending an editing session in a
UITextFieldobject by leaving its bounds.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventEditingDidEndOnExitA touch ending an editing session in a
UITextFieldobject.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventAllTouchEventsAll touch events.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventAllEditingEventsAll editing touches for
UITextFieldobjects.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventApplicationReservedA range of control-event values available for application use.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventSystemReservedA range of control-event values reserved for internal framework use.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlEventAllEventsAll events, including system events.
Available in iOS 2.0 and later.
Declared in
UIControl.h.
Discussion
You set up a control so that it sends an action message to a target object by associating both target and action with one or more control events. To do this, send addTarget:action:forControlEvents:: to the control for each target-action pair you want to specify.
Vertical Content Alignment
The vertical alignment of content (text and images) within a control.
typedef enum {
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
} UIControlContentVerticalAlignment;
Constants
UIControlContentVerticalAlignmentCenterAligns the content vertically in the center of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentVerticalAlignmentTopAligns the content vertically at the top in the control (the default).
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentVerticalAlignmentBottomAligns the content vertically at the bottom in the control
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentVerticalAlignmentFillAligns the content vertically to fill the content rectangle; images may be stretched.
Available in iOS 2.0 and later.
Declared in
UIControl.h.
Discussion
You use these constants as the value of the contentVerticalAlignment property.
Horizontal Content Alignment
The horizontal alignment of content (text and images) within a control.
typedef enum {
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
} UIControlContentHorizontalAlignment;
Constants
UIControlContentHorizontalAlignmentCenterAligns the content horizontally in the center of the control.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentHorizontalAlignmentLeftAligns the content horizontally from the left of the control (the default).
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentHorizontalAlignmentRightAligns the content horizontally from the right of the control
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlContentHorizontalAlignmentFillAligns the content horizontally to fill the content rectangles; text may wrap and images may be stretched.
Available in iOS 2.0 and later.
Declared in
UIControl.h.
Discussion
You use these constants as the value of the contentHorizontalAlignment property.
Control State
The state of a control; a control can have more than one state at a time. States are recognized differently depending on the control. For example, a UIButton instance may be configured (using the setImage:forState: method) to display one image when it is in its normal state and a different image when it is highlighted.
enum {
UIControlStateNormal = 0,
UIControlStateHighlighted = 1 << 0,
UIControlStateDisabled = 1 << 1,
UIControlStateSelected = 1 << 2,
UIControlStateApplication = 0x00FF0000,
UIControlStateReserved = 0xFF000000
};
Constants
UIControlStateNormalThe normal, or default state of a control—that is, enabled but neither selected nor highlighted.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlStateHighlightedHighlighted state of a control. A control enters this state when a touch enters and exits during tracking and when there is a touch up event. You can retrieve and set this value through the
highlightedproperty.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlStateDisabledDisabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the
enabledproperty.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlStateSelectedSelected state of a control. For many controls, this state has no effect on behavior or appearance. But other subclasses (for example, the
UISegmentedControlclass) may have different appearance depending on their selected state. You can retrieve and set this value through theselectedproperty.Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlStateApplicationAdditional control-state flags available for application use.
Available in iOS 2.0 and later.
Declared in
UIControl.h.UIControlStateReservedControl-state flags reserved for internal framework use.
Available in iOS 2.0 and later.
Declared in
UIControl.h.
Declared In
UITouch.hUIControlState
The bit-mask type for control-state constants.
typedef NSUInteger UIControlState;
Discussion
The constants are listed in “Control State.” Use the state property to retrieve the current state bits set for a control.
Availability
- Available in iOS 2.0 and later.
Declared In
UIControl.h© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-09-28)