辅助功能无法看到SWTableViewCell的实用程序按钮

时间:2015-10-08 09:54:15

标签: ios accessibility xcode7 xctest ui-testing

我正在使用SWTableviewCell,我可以向左滑动并显示带有XCTest和辅助功能的实用程序按钮,但找不到这些元素却没有运气。使用辅助功能检查器进行双重检查。

由于SWTableViewCell中的实用程序按钮是常规UIButton,因此它应该可以直接使用。还尝试设置按钮的可访问性标签。

任何帮助将不胜感激。谢谢!

更新: 这是我的代码:

    // This works
    app = XCUIApplication()
    let firstCell = app.tables.cells.elementBoundByIndex(0)
    firstCell.swipeLeft()

    // Following 2 TODO's doesn't work
    // TODO: tap More button
    firstCell.buttons["More"].tap()
    XCTAssertEqual(app.sheets.count, 1, "'More' action sheet should be present")
    // TODO: assert sheet doesn't contain Lock/Unlock button
    let sheet = app.sheets.element
    let lockButton = sheet.buttons["Lock"]
    sheet.buttons["Cancel"].tap()

2 个答案:

答案 0 :(得分:1)

为我的UIAccessibilityContainer子类添加覆盖UITableViewCell方法,如下所示:

- (NSArray *)accessibilityElements {
    if (_accessibilityElements == nil) {
        _accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 2];
        [_accessibilityElements addObject: self.miscLabel];
        [_accessibilityElements addObject: self.titleLabel];
    }
    return _accessibilityElements;
}   

- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
    return _accessibilityElements[index];
}   

- (NSInteger)indexOfAccessibilityElement:(id)element {
    return [_accessibilityElements indexOfObject: element];
}

注意_accessibilityElements是我的单元格的实例变量:

@interface MyCell : SWTableViewCell
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *miscLabel;
...
@property (strong, nonatomic) NSMutableArray *accessibilityElements;

@end

然后,实现了SWTableViewCellDelegate方法来向辅助功能添加和删除实用程序按钮:

- (void)swipeableTableViewCellDidEndScrolling:(MyCell *)cell {
    NSMutableArray *utilityButtons = cell.accessibilityElements;
    if (cell.isUtilityButtonsHidden) {
        [utilityButtons removeObjectsInArray: cell.rightUtilityButtons];
    }
    else {
        [utilityButtons addObjectsFromArray: cell.rightUtilityButtons];
    }
}

答案 1 :(得分:1)

在SWTableViewCell.h中,在文件末尾添加以下代码行。

// Accessibility
@property (strong, nonatomic) NSMutableArray *accessibilityElements;
- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;

以及SWTableViewCell.m中的以下行

#pragma mark Accessibilty

- (void)addElementForAccesiblilty:(id)element
{
    if (![self.accessibilityElements containsObject:element]) {
        [self.accessibilityElements addObject:element];
    }
}

- (void)removeElementFromAccesiblilty:(id)element
{
    [self.accessibilityElements removeObject:element];
}

- (NSArray *)accessibilityElements {
    if (!_accessibilityElements) {
        _accessibilityElements = [[NSMutableArray alloc] initWithCapacity: 0];
        [self updateAccessibilityElementsForView:self];
    }
    return _accessibilityElements;
}

- (void)updateAccessibilityElementsForView:(UIView*)v
{
    for (UIView *subView in v.subviews)
    {
        if ([subView isKindOfClass:[UILabel class]] || [subView isKindOfClass:[UIButton class]]) {
            [_accessibilityElements addObject:subView];
        }
        [self updateAccessibilityElementsForView:subView];
    }
}

- (nullable id)accessibilityElementAtIndex:(NSInteger)index {
    id element = nil;
    @synchronized(_accessibilityElements) {
        element = _accessibilityElements[index];
    }
    return element;
}

- (NSInteger)indexOfAccessibilityElement:(id)element {
    NSInteger index = NSNotFound;
    @synchronized(_accessibilityElements) {
        index = [_accessibilityElements indexOfObject: element];
    }
    return index;
}


- (void)updateAccessibleElementsForUtilityButtons {
    switch (self.cellState) {
        case kCellStateLeft:
        {
            if (self.isUtilityButtonsHidden) {
                [self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
            }
            else {
                [self.accessibilityElements addObjectsFromArray: self.leftUtilityButtons];
            }
            break;
        }
        case kCellStateRight:
        {
            if (self.isUtilityButtonsHidden) {
                [self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
            }
            else {
                [self.accessibilityElements addObjectsFromArray: self.rightUtilityButtons];
            }
            break;
        }
        default: {
            [self.accessibilityElements removeObjectsInArray: self.rightUtilityButtons];
            [self.accessibilityElements removeObjectsInArray: self.leftUtilityButtons];
            break;
        }
    }
}
scrollViewDidEndScrollingAnimation scrollViewDidEndDecelerating 委托方法中调用方法 updateAccessibleElementsForUtilityButtons ,如下所示

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateCellState];
    [self updateAccessibleElementsForUtilityButtons];
    if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
        [self.delegate swipeableTableViewCellDidEndScrolling:self];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self updateCellState];
    [self updateAccessibleElementsForUtilityButtons];
    if (self.delegate && [self.delegate respondsToSelector:@selector(swipeableTableViewCellDidEndScrolling:)]) {
        [self.delegate swipeableTableViewCellDidEndScrolling:self];
    }
}

这会将所有按钮和标签添加为单元格中的可访问元素。但是如果你想按照方法添加/删除任何元素用户

- (void)addElementForAccesiblilty:(id)element;
- (void)removeElementFromAccesiblilty:(id)element;