2014-01-30 120 views
6

如何將accessibilityLabel添加到UIAlertView按鈕?如何將`accessibilityLabel`添加到`UIAlertView`按鈕?

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" 
               message: @"message!" 
               delegate: nil  
             cancelButtonTitle: @"cancelButton" 
             otherButtonTitles: @"otherButton"]; 

[alert show]; 
+2

默認可訪問性有什麼問題?我不認爲有一種(官方)方式來改變它。 – Wain

回答

1

唯一這樣你可以做到這一點是找到UIAlertView中子視圖中的UIButtons:

for (id button in self.alertView.subviews){ 
    if ([button isKindOfClass:[UIButton class]]){ 
     ((UIButton *)button).accessibilityLabel = @"your custom text"; 
    } 
} 

然而,這是這樣做,因爲沒有一個公共的API來訪問這些UIButtons的唯一途徑,這是因爲蘋果不希望您訪問它們。訪問UIAlertView類的內部視圖是Apple不允許的內容,它很可能會在App Store審閱過程中拒絕您的應用。

如果您確實需要使用自定義accessibilityLabel來設置UIButton,則應該考慮設計自定義警報視圖,而不是使用Apple UIAlertView類。

6

根據Apple's documentation(搜索 '使警報瀏覽殘疾人專用'),AlertViews是 '默認訪問'。這一點以及按鈕不可編輯的事實意味着您可能不應該嘗試自己更改accessibilityLabel。默認情況下,他們使用按鈕的標題和單詞'按鈕',這應該很好。

警報視圖的輔助功能屬於警報標題,警報消息和按鈕標題。如果VoiceOver已激活,則會在顯示提示時顯示「alert」字樣,然後說出其標題,如果設置了該消息,則會顯示其消息。當用戶點擊一個按鈕,VoiceOver會說出它的名稱和單詞「按鈕」。當用戶點擊一個文本字段,VoiceOver會說出它的價值和「文本字段」或「安全文本字段。」

0

晚了,但也許對某人有用。

您可以使用索引訪問警報按鈕。例如,您可以像在目標C通過以下方式在屏幕找到第二個按鈕:

- (void)getAlertActionButton:(XCUIApplication *)app { 
    // any code 
    XCUIElement *secondAlertButton = [[[app.alerts otherElements] buttons] elementBoundByIndex:1]; 
    // any code  
} 

要找到你的警告窗口層次使用調試模式。