如何将accessibilityIdentifier设置为UIAlertController?

编程入门 行业动态 更新时间:2024-10-10 04:23:33
本文介绍了如何将accessibilityIdentifier设置为UIAlertController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这就是我简单地创建 UIAlertController 并在屏幕上显示的方式:

This is how I simply create UIAlertController and present it on the screen:

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 下访问它的方式:

and this is how I access it under UITests:

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

但是我想在那里设置自定义标识符并通过 firstName 访问它:

but I would like to set there custom identifier and access this by firstName like this:

emailAlert = app.alerts["firstName"]

有可能吗?

推荐答案

我想出办法的唯一方法就是使用Apple的私有API。使用此超级密钥在 UIAlertAction 对象上调用 valueForKey :__ representer获取什么称为 _UIAlertControllerActionView 。

The only way I figured out to do this was to use Apple's private APIs. You call valueForKey on the UIAlertAction object with this super secret key: "__representer" to get whats called a _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 将不存在。在我的项目的旁注中,我使用以下扩展来使事情更容易/更易读:

This has to be done in the completion handler because that that _UIAlertControllerActionView won't exist until the view is presented. On a side note in my project I used these following extensions to make things easier / more readable:

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 } }

因此上面的代码将被重写:

So the above code would be rewritten:

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),否则Apple可能会给你打屁股。

My first attempt involved trying to navigate the view hierarchy but that became difficult since UIAlertControllerActionView was not a part of the public API. Anyway I'd probably would try to ifdef out the valueForKey("__representer") for builds submitted for the app store or Apple might give you a spanking.

更多推荐

如何将accessibilityIdentifier设置为UIAlertController?

本文发布于:2023-11-25 21:45:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631465.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   如何将   accessibilityIdentifier   UIAlertController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!