Supporting Actions

NSAccessibility defines three methods for accessing an object’s actions:

The first method returns an array of action names supported by the accessibility object, the second returns a localized string describing a particular action, and the third performs a particular action.

When supporting an action in a subclass, you need to override all three methods. In the accessibilityActionNames method, you need to invoke the superclass’s implementation and append your new action. This allows an assistive application to get an accurate list of all actions you support. In the other two methods, compare the action name to those your subclass supports; if no match is found, invoke the superclass’s implementation. Listing 1 shows sample implementations of these methods that add a new action named @"Boing".

Listing 1  Supporting a new action

static NSString *MyBoingActionName = @"Boing";
 
- (NSArray *)accessibilityActionNames
{
    static NSArray *actions = nil;
    if (actions == nil) {
        actions = [[[super accessibilityActionNames]
                arrayByAddingObject:MyBoingActionName] retain];
    }
    return actions;
}
 
- (NSString *)accessibilityActionDescription:(NSString *)action
{
    if ( [action isEqualToString:MyBoingActionName] )
        return NSLocalizedString(@"BoingDescription",
                    @"Performs the Boing action");
    else
        return [super accessibilityActionDescription:action];
}
 
- (void)accessibilityPerformAction:(NSString *)action
{
    if ( [action isEqualToString:MyBoingActionName] )
        [self doBoing];
    else
        [super accessibilityPerformAction:action];
}

When performing an action, the subclass’s implementation ideally should invoke the same methods that are invoked if the action is performed directly from the user interface.