首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在UIBarButtonItem上以编程方式设置辅助功能标识符

在UIBarButtonItem上以编程方式设置辅助功能标识符
EN

Stack Overflow用户
提问于 2013-12-12 20:37:44
回答 4查看 28.7K关注 0票数 23

可访问性标识符是开发人员为GUI对象生成的ID,可用于自动化测试。

UIBarButtonItem不实现UIAccessibilityIdentification。但是,我是否可以分配一个可访问性标识符?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-12-26 03:13:20

您可以创建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);
    }
}

希望这能有所帮助。

票数 17
EN

Stack Overflow用户

发布于 2013-12-19 01:45:57

从iOS 5开始,你可以这样做:

UIBarButtonItem *btn = [[UIBarButtonItem alloc] init...;
btn.accessibilityLabel = @"Label";
票数 5
EN

Stack Overflow用户

发布于 2013-12-24 17:01:46

如果你在里面创建了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", @"")];
    }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20543804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档