2016-06-30 37 views
6

這就是我簡單地創建UIAlertController並將其顯示在屏幕上:如何將accessibilityIdentifier設置爲UIAlertController?

private class func showAlertWithTitle(title: String, message: String) { 

    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert) 
    //alert.accessibilityLabel = "my string here"  //doesnt work 
    let action = UIAlertAction(title: "OK", style: .Default) { action in 
     alert.dismissViewControllerAnimated(true, completion: nil) 
    } 

    alert.addAction(action) 
    UIStoryboard.topViewController()?.presentViewController(alert, animated: true, completion: nil) 
} 

,這是我如何訪問它下UITests

emailAlert = app.alerts["First Name"] //for title "First Name" 

,但我想設置有定製標識並通過firstName這樣訪問:

emailAlert = app.alerts["firstName"] 

難道是po ssible?

回答

3

我想出的方法是使用Apple的私有API。您使用這個超級密鑰:"__representer"UIAlertAction對象上致電valueForKey即可獲得稱爲_UIAlertControllerActionView的稱謂。

let alertView = UIAlertController(title: "This is Alert!", message: "This is a message!", preferredStyle: .Alert) 
    let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 

    alertView.addAction(okAction) 

    self.presentViewController(alertView, animated: true, completion: { 
     let alertButton = action.valueForKey("__representer") 
     let view = alertButton as? UIView 
     view?.accessibilityIdentifier = "okAction_AID" 
    }) 

這在完成處理工作要做,因爲這是_UIAlertControllerActionView將不存在,直到視圖呈現。在我的項目一個側面說明我用以下這些擴展,使事情變得更容易/更易讀:

extension UIAlertController { 
    func applyAccessibilityIdentifiers() 
    { 
     for action in actions 
     { 
      let label = action.valueForKey("__representer") 
      let view = label as? UIView 
      view?.accessibilityIdentifier = action.getAcAccessibilityIdentifier() 
     } 

    } 

} 

extension UIAlertAction 
{ 
    private struct AssociatedKeys { 
     static var AccessabilityIdentifier = "nsh_AccesabilityIdentifier" 
    } 

    func setAccessibilityIdentifier(accessabilityIdentifier: String) 
    { 
     objc_setAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier, accessabilityIdentifier, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) 
    } 

    func getAcAccessibilityIdentifier() -> String? 
    { 
     return objc_getAssociatedObject(self, &AssociatedKeys.AccessabilityIdentifier) as? String 
    } 
} 

所以上面的代碼會被重寫:

let alertView = UIAlertController(title: NSLocalizedString("NMN_LOGINPAGECONTROLLER_ERROR_TITLE", comment: ""), message: message as String, preferredStyle:.Alert) 
    let okAction = UIAlertAction(title: NSLocalizedString("NMN_OK", comment: ""), style: .Default, handler: nil) 
    okAction.setAccessibilityIdentifier(InvalidLoginAlertView_AID) 


    alertView.addAction(okAction) 


    self.presentViewController(alertView, animated: true, completion: { 
     alertView.applyAccessibilityIdentifiers() 
    }) 

我第一次嘗試涉案試圖瀏覽查看層次結構,但由於UIAlertControllerActionView不是公共API的一部分,所以變得困難。無論如何,我可能會嘗試如果出於valueForKey("__representer")爲應用程序商店提交的構建或蘋果可能會給你一個打屁股。

+0

這有效。我剛開了一個蘋果的bug,希望如果我們中的很多人抱怨他們會做點什麼。 – user1366265

+0

任何想法如何設置標題和消息的輔助功能標識符? – user1366265

+0

對不起,我不確定。只需要爲自動化目的設置輔助功能標識符,以便從不需要訪問標題或消息。 –

-1

從蘋果文檔...

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html

殘疾人專用

警報的觀點使警報瀏覽在默認情況下使用。 警報視圖的輔助功能屬於警報標題,警報消息和按鈕標題。如果VoiceOver已激活,則會在顯示提示時顯示「提醒」一詞,然後說出其標題,如果設置了則顯示其消息。當用戶點擊一個按鈕時,VoiceOver會說出其標題和單詞「按鈕」。當用戶點擊文本字段時,VoiceOver會說出其值和「文本字段」或「安全文本字段」。

+0

這沒有提及如何設置輔助功能標識符。 – user1366265

+0

不,但它是Apple推薦的訪問警報的方式。 – tanya

相關問題