2013-12-12 11 views

Trả lời

13

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.

+0

chính xác những gì tôi cần, cảm ơn! – Chris

+0

Vui vì tôi có thể giúp! –

+5

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

4

Tính đến iOS 5 bạn có thể làm điều đó như thế này:

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

Mã định danh khả năng truy cập có giống với nhãn không? – Chris

+0

không chắc chắn ý bạn là gì, nhưng tôi sẽ thử: mặc định là không, bạn phải tự mình đặt. –

+0

u có thể sử dụng thẻ thay vì trợ năng và cũng nhận được đối tượng từ thẻ. –

3

Nếu bạn có UIToolbar tạo bên trong rằng nếu bạn muốn tạo nhiều UIBarButtonItem programatically sau đó nó có thể được truy cập như thế và thiết lập accessibilityLabel cũng như mà dưới đây: -

-(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", @"")]; 
    } 
2

Phân lớp UIBarButtonItem là giải pháp tốt. Tùy thuộc vào nhu cầu của bạn, tuy nhiên, có thể có ý nghĩa hơn khi chỉ cần chỉ định accessibilityIdentifier cho hình ảnh tùy chỉnh của UIBarButtonItem, giả sử UIBarButtonItem của bạn sử dụng hình ảnh tùy chỉnh.

Các vấn đề liên quan