2013-12-12 2 views

답변

13

당신은 UIBarButtonItem 하위 클래스 수와 그 서브 클래스에 UIAccessibilityIdentification 프로토콜을 구현한다. BarButtonWithAccesibility.h에서

:

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification> 

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

accessibilityIdentifier 속성을 정의하고이 프로토콜을 준수하는 유일한 (엄격한) 요구 사항.

#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에 액세스 할 수 있는지 확인 수 :보기 컨트롤러에서 이제

, 당신이 UIToolbar를 설정하고 서브 클래스 UIBarButtonItem을 추가 할 수 있습니다,의는 viewDidLoad에 가정 해 봅시다

- (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에서 문제가 해결 된 것처럼 보였습니다. 이제 'accessibilityIdentifier'도 'UIBarButtonItem'과 함께 사용할 수 있습니다. – fabb

4

에서 iOS 5의로서 당신은 이런 식으로 작업을 수행 할 수 있습니다

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

접근성 식별자는 레이블과 동일합니까? – Chris

+0

무슨 뜻인지 잘 모르겠지만 시도해 보겠습니다. 기본적으로는 아무 것도 아니므로 직접 설정해야합니다. –

+0

u는 접근성 대신 태그를 사용할 수 있으며 태그에서 객체를 가져올 수도 있습니다. –

3

을 당신이 프로그래밍 방식으로 다음이 그런 식으로 액세스 할 수있는 여러 UIBarButtonItem을 만들려면과 같은뿐만 아니라 accessibilityLabel를 설정하면 그 안에 생성 UIToolbar이있는 경우 아래에 있음 : -의이 BarButtonWithAccesibility을 말할 수,

-(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을 할당하는 것이 더 합리적 일 수 있습니다.

관련 문제