在UIBarButtonItem上以编程方式设置可访问性标识符 [英] Setting accessibility identifier programmatically on UIBarButtonItem

查看:88
本文介绍了在UIBarButtonItem上以编程方式设置可访问性标识符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The accessibility identifier is a developer generated ID for GUI objects, which can be used for automation tests.

UIBarButtonItem 未实现 UIAccessibilityIdentification .但是,我可以分配一个可访问性标识符吗?

A UIBarButtonItem does not implement UIAccessibilityIdentification. However is there a possibility that I can assign an accessibility identifier?

推荐答案

您可以将 UIBarButtonItem 子类化,并在该子类中实现 UIAccessibilityIdentification 协议,比如说 BarButtonWithAccesibility.

You could subclass UIBarButtonItem and implement the UIAccessibilityIdentification protocol in that subclass, lets's say BarButtonWithAccesibility.

BarButtonWithAccesibility.h 中:

@interface BarButtonWithAccesibility : UIBarButtonItem<UIAccessibilityIdentification>

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

遵守此协议的唯一(严格)要求是定义 accessibilityIdentifier 属性.

The only (strict) requirement for adhering to this protocol is defining the accessibilityIdentifier property.

现在在视图控制器中,例如在 viewDidLoad 中,您可以设置UIToolbar并添加子类UIBarButtonItem:

Now in your view controller, let's say in viewDidLoad, you could set up a UIToolbar and add your subclassed 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 :

And within the buttonPressed: you could verify that you have access to the accessibilityIdentifier:

- (void)buttonPressed:(id)sender{
    if ([sender isKindOfClass:[BarButtonWithAccesibility class]]) {
        BarButtonWithAccesibility *theButton = (BarButtonWithAccesibility *)sender;
        NSLog(@"My accesibility identifier is: %@", theButton.accessibilityIdentifier);
    }
}

希望这会有所帮助.

这篇关于在UIBarButtonItem上以编程方式设置可访问性标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆