Skip to content
This repository has been archived by the owner on Dec 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #234 from matrix-org/alert_access_id
Browse files Browse the repository at this point in the history
MXKAlert: Support an accessibility identifier
  • Loading branch information
giomfo authored Jan 27, 2017
2 parents 49d013a + 66b8d02 commit 00391df
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
1 change: 1 addition & 0 deletions MatrixKit/Views/MXKAlert.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef void (^blockMXKAlert_onClick)(MXKAlert *alert);
typedef void (^blockMXKAlert_textFieldHandler)(UITextField *textField);

@property(nonatomic) NSInteger cancelButtonIndex; // required to dismiss cusmtomAlert on iOS < 8 (default is -1).
@property(nonatomic) NSString *mxkAccessibilityIdentifier; // Supported only on iOS >= 8.0.
@property(nonatomic, weak) UIView *sourceView;

- (id)initWithTitle:(NSString *)title message:(NSString *)message style:(MXKAlertStyle)style;
Expand Down
59 changes: 52 additions & 7 deletions MatrixKit/Views/MXKAlert.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,22 @@ - (NSInteger)addActionWithTitle:(NSString *)title style:(MXKAlertActionStyle)sty
if ([_alert isKindOfClass:[UIAlertController class]])
{
index = [(UIAlertController *)_alert actions].count;

UIAlertAction* action = [UIAlertAction actionWithTitle:title
style:(UIAlertActionStyle)style
handler:^(UIAlertAction * action)
handler:^(UIAlertAction * action) {

if (handler)
{
handler(self);
}

}];

if (_mxkAccessibilityIdentifier)
{
if (handler)
{
handler(self);
}
}];
action.accessibilityLabel = [NSString stringWithFormat:@"%@Action%@", _mxkAccessibilityIdentifier, title];
}

[(UIAlertController *)_alert addAction:action];
}
Expand Down Expand Up @@ -124,11 +131,49 @@ - (NSInteger)addActionWithTitle:(NSString *)title style:(MXKAlertActionStyle)sty
return index;
}

- (void)setMxkAccessibilityIdentifier:(NSString *)mxkAccessibilityIdentifier
{
_mxkAccessibilityIdentifier = mxkAccessibilityIdentifier;

// Consider only iOS >= 8.0
if ([_alert isKindOfClass:[UIAlertController class]])
{
UIAlertController *alertController = (UIAlertController *)_alert;

alertController.view.accessibilityIdentifier = mxkAccessibilityIdentifier;

for (UIAlertAction *action in alertController.actions)
{
action.accessibilityLabel = [NSString stringWithFormat:@"%@Action%@", mxkAccessibilityIdentifier, action.title];
}

NSArray *textFieldArray = alertController.textFields;
for (NSUInteger index = 0; index < textFieldArray.count; index++)
{
UITextField *textField = textFieldArray[index];
textField.accessibilityIdentifier = [NSString stringWithFormat:@"%@TextField%tu", mxkAccessibilityIdentifier, index];
}
}
}

- (void)addTextFieldWithConfigurationHandler:(blockMXKAlert_textFieldHandler)configurationHandler
{
if ([_alert isKindOfClass:[UIAlertController class]])
{
[(UIAlertController *)_alert addTextFieldWithConfigurationHandler:configurationHandler];
UIAlertController *alertController = (UIAlertController *)_alert;

[alertController addTextFieldWithConfigurationHandler:configurationHandler];

if (_mxkAccessibilityIdentifier)
{
// Define an accessibility id for each field.
NSArray *textFieldArray = alertController.textFields;
for (NSUInteger index = 0; index < textFieldArray.count; index++)
{
UITextField *textField = textFieldArray[index];
textField.accessibilityIdentifier = [NSString stringWithFormat:@"%@TextField%tu", _mxkAccessibilityIdentifier, index];
}
}
}
else if ([_alert isKindOfClass:[UIAlertView class]])
{
Expand Down

0 comments on commit 00391df

Please sign in to comment.