2013-12-12 8 views
18

にアクセシビリティ識別子は、開発者が自動テストのために使用することができるGUIオブジェクトのIDを生成しています。設定アクセシビリティ識別子プログラムUIBarButtonItem

UIBarButtonItemUIAccessibilityIdentificationを実装していません。しかし、アクセシビリティ識別子を割り当てることができる可能性はありますか?

答えて

13

あなたは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); 
    } 
} 

は、この情報がお役に立てば幸いです。

+0

まさに私が必要なもの、感謝! – Chris

+0

うれしい私は助けることができました! –

+5

iOS 8が問題を修正したように見え、 'UIBarButtonItem'でも' accessibilityIdentifier'を使うことができるようになりました。 – fabb

4

あなたはこのようにそれを行うことができますiOSの5のとおり:

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

アクセシビリティ識別子はラベルと同じですか? – Chris

+0

あなたは何を意味するのか分かりませんが、試してみましょう:デフォルトでは無名です。自分で設定する必要があります。 –

+0

uはアクセシビリティの代わりにタグを使用できますし、タグからオブジェクトを取得することもできます。 –

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

サブクラスUIBarButtonItemは良い解決策です。あなたのニーズに応じて、しかし、それはあなたのUIBarButtonItemは、カスタムイメージを使用すると仮定すると、単にあなたのUIBarButtonItemのカスタムイメージにaccessibilityIdentifierを割り当てるにはより多くの意味を行うことができます。

関連する問題