Bạn có thể phân lớp UIBarButtonItem
và thực hiện các giao thức UIAccessibilityIdentification
trong lớp con đó, cho phép nói rằng BarButtonWithAccesibility
.
Trong BarButtonWithAccesibility.h
:
@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>
@property(nonatomic, copy) NSString *accessibilityIdentifier NS_AVAILABLE_IOS(5_0);
duy nhất (chặt chẽ) yêu cầu về tôn trọng giao thức này được định accessibilityIdentifier
tài sản.
Bây giờ trong điều khiển điểm của bạn, giả sử trong viewDidLoad
, bạn có thể thiết lập một UIToolbar và thêm UIBarButtonItem subclassed của bạn:
#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];
}
Và trong buttonPressed:
bạn có thể xác minh rằng bạn có quyền truy cập vào các accessibilityIdentifier
:
- (void)buttonPressed:(id)sender{
if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {
BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;
NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);
}
}
Hy vọng điều này sẽ hữu ích.
Nguồn
2013-12-25 19:13:19
chính xác những gì tôi cần, cảm ơn! – Chris
Vui vì tôi có thể giúp! –
Có vẻ như iOS 8 đã khắc phục được sự cố và 'accessIdentifier' bây giờ cũng có thể được sử dụng với' UIBarButtonItem'. – fabb