Ответ 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);
}
}
Надеюсь, что это поможет.