如何识别UIAutomation中添加到UIButton的子视图(图像视图)?

时间:2014-08-14 06:04:09

标签: ios objective-c iphone instruments ios-ui-automation

我有一个UIButton,按钮background设置为image。添加 按钮右上角的image view我设置了它 单击按钮以显示选择时的不同图像。

当我尝试使用automate instruments时。我没有看到任何subviews image viewsUIAutomator方法中的{logElementTree()}未显示 任何image views

如何识别image views中添加为subview的{​​{1}}?

这是代码。

UIButton

3 个答案:

答案 0 :(得分:2)

您必须设置accessibilityEnabled& accessibilityLabel。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];

button.accessibilityEnabled = YES;
button.accessibilityLabel =@"My Button";

[button setBackgroundImage:[UIImage imageNamed:@“testImage.png”] forState:UIControlStateNormal];


UIImageView *imageview = [[UIImageView alloc] initWithImage:image];

imageview.accessibilityEnabled = YES;
imageview.accessibilityLabel = @"My Image";

imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];

enter image description here

Refer this tutorial

答案 1 :(得分:1)

如果你有一个UIImageView子视图,那么Sweet Angel的答案就足够了。但是,我建议使用tag来识别子视图。

imageView.tag = 1346;//Any arbitrary number (I am unsure if there are any limitations to the values you can use, but all 4 digit numbers work fine for me).

然后获取imageView:

UIImageView* imageView = (UIImageView*)[button viewWithTag:1346];

就是这样。

答案 2 :(得分:0)

如果您想在按钮上识别图像视图,请尝试以下代码:

- (IBAction)didTapButton:(UIButton *)sender
{
    for (id view in sender.subviews)
    {
        if ([view isKindOfClass:[UIImageView class]])
        {
            NSLog(@"Image view captured.....");
        }
    }
}