【问题标题】:focus on accessible UI element in cocoa programmatically以编程方式关注可可中可访问的 UI 元素
【发布时间】:2012-10-10 09:02:59
【问题描述】:

我有一个带有按钮的工具栏和一个带有标题框的 NSView。当窗口打开并且旁白打开时,我希望焦点位于标题框上。目前它位于工具栏按钮上,因为它在可访问性层次结构中可能更高。我应该怎么做才能将焦点放在框上而不是按钮上?

谢谢

【问题讨论】:

    标签: cocoa osx-lion accessibility


    【解决方案1】:

    解决方案是像这样访问windowDidLoad 中的窗口可访问性层次结构:

    id windowElement =  NSAccessibilityUnignoredDescendant(self.window);
    NSArray *windowElements = [windowElement accessibilityAttributeValue:NSAccessibilityChildrenAttribute];
    

    之后,应该在windowElements 数组中找到应该在层次结构中排在第一位的元素。假设我们找到它并且它的索引是firstElIndex 那么:

        NSMutableArray *mutArray = [NSMutableArray arrayWithArray:windowElements];
            id firstElement = [[mutArray objectAtIndex:firstElIndex] retain];
    
            [mutArray removeObject:firstElement];
            [mutArray insertObject:firstElement atIndex:0];
    
            [windowElement accessibilitySetOverrideValue:mutArray forAttribute:NSAccessibilityChildrenAttribute];
            [windowElement accessibilitySetOverrideValue:mutArray forAttribute:NSAccessibilityVisibleChildrenAttribute];
            [windowElement accessibilitySetOverrideValue:mutArray forAttribute:NSAccessibilitySelectedChildrenAttribute];
            [firstElement release];
    

    最后,我们用新数组覆盖windowElement children 属性,我们希望焦点放在其中的元素将位于首位。

    这是一个非常简单的例子。但这是要走的路。

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 2013-07-09
      • 1970-01-01
      • 2020-06-11
      • 2011-01-13
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多