Подтвердить что ты не робот

Установка идентификатора доступности программно на UIBarButtonItem

Идентификатор доступности - это сгенерированный разработчиком идентификатор для объектов GUI, который может использоваться для тестов автоматизации.

A UIBarButtonItem не реализует UIAccessibilityIdentification. Однако есть ли вероятность, что я могу назначить идентификатор доступности?

4b9b3361

Ответ 1

Вы можете подклассом UIBarButtonItem и реализовать протокол UIAccessibilityIdentification в этом подклассе, скажем BarButtonWithAccesibility.

В BarButtonWithAccesibility.h:

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>

@property(nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0);

Единственным (строгим) требованием для соблюдения этого протокола является определение свойства accessibilityIdentifier.

Теперь в вашем контроллере просмотра, скажем, в viewDidLoad, вы можете настроить UIToolbar и добавить свой подклассов UIBarButtonItem:

#import "BarButtonWithAccesibility.h"

- (void)viewDidLoad{

    [super viewDidLoad];

    UIToolbar *toolbar = [[UIToolbar alloc]  initWithFrame:CGRectMake(0, 0, 320, 44)];

    BarButtonWithAccesibility *myBarButton = [[BarButtonWithAccesibility alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonPressed:)];
    myBarButton.accessibilityIdentifier = @"I am a test button!";

    toolbar.items = [[NSArray alloc] initWithObjects:myBarButton, nil];
    [self.view addSubview:toolbar];
}

И в buttonPressed: вы можете убедиться, что у вас есть доступ к accessibilityIdentifier:

- (void)buttonPressed:(id)sender{
    if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {
        BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;
        NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);
    }
}

Надеюсь, что это поможет.

Ответ 2

Как и в iOS 5, вы можете сделать это следующим образом:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] init...;
btn.accessibilityLabel = @"Label";

Ответ 3

Если у вас есть UIToolbar, созданный внутри, если вы хотите создать несколько UIBarButtonItem программно, тогда к нему можно получить доступ так же, как и установить accessibilityLabel, как это показано ниже: -

    -(void)viewDidAppear:(BOOL)animated
    {
        UIBarButtonItem *infoButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"info" style:UIBarButtonItemStyleBordered  target:self action:@selector(infoButtonClicked)];
        [self.customToolBar setItems:[NSArray arrayWithObject:infoButtonItem]];
//Here if you have muliple you can loop through it 
       UIView *view = (UIView*)[self.customToolBar.items objectAtIndex:0];
    [view setAccessibilityLabel:NSLocalizedString(@"Test", @"")];
    }

Ответ 4

Подклассификация UIBarButtonItem является хорошим решением. Однако, в зависимости от ваших потребностей, имеет смысл просто назначить accessibilityIdentifier пользовательскому изображению вашего UIBarButtonItem, если ваш UIBarButtonItem использует настраиваемый образ.