0

There are 3 buttons in my app, when voice over is turned on and the user swipes to the right, the next button is selected.

Note - The button accessibility focus is different from button pressed.

Reason for asking - I want the app to announce "AAA" when the button 1 is selected and "BBB" when button 1 is pressed.

Questions

  • How can the above be achieved ?
  • Is there a way to call a method when a button is selected.
user1046037
  • 16,755
  • 12
  • 92
  • 138

2 Answers2

3

The View (in this case button) needs to implement the protocol UIAccessibilityFocus

- (void)accessibilityElementDidBecomeFocused
- (void)accessibilityElementDidLoseFocus
- (BOOL)accessibilityElementIsFocused
user1046037
  • 16,755
  • 12
  • 92
  • 138
0

Here is a few ObjectiveC lines of code to perform what you want to do :

// Create button
UIButton *b = [[UIButton alloc] init];
// Set it's title
[b setTitle:@"AAA" forState:UIControlStateNormal];
// Set the label, i.e. it's name
[b setAccessibilityLabel:NSLocalizedString(@"AAA", @"")];
// Set button action description
[b setAccessibilityHint:NSLocalizedString(@"AAA button action description", @"")];
// Ad the button target, it's action
[b addTarget:self action:@selector(yourMethod:) forControlEvents:UIControlEventTouchUpInside];
// Add an observer
[b addObserver:self forKeyPath:@"state" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];


// Implement this method in your controller
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if (object == self.b && [keyPath isEqualToString:@"state"]) {
        if (self.b.state == UIControlStateSelected) {
            // DO WHAT YOU WANT TO DO ON SELECTION
        }
    }
}

Take a look at Apple accessibility Guide for more infos.

Dulgan
  • 6,674
  • 3
  • 41
  • 46
  • How would I know when the button is selected (not button press) ? – user1046037 Nov 27 '14 at 10:19
  • I updated my answer using KVO to observe the button state, maybe there is an other way but I don't now any, take a look at Apple Accessibily Guides to get obviously better ways to do this – Dulgan Nov 27 '14 at 11:16