我们正在尝试使用Xamarin.iOS中的扩展为UIAlertController设置AutomationId。但是在GetAccessibilityIdentifier方法下,应用程序在var valuePtr = objc_getAssociatedObject(alertAction.Handle, DescriptiveName.Handle);
处不一致地崩溃了。也不知道为什么会崩溃。
以下是代码示例:
public static class UIAlertControllerExtension
{
public static void ApplyAccessibilityIdentifiers(this UIAlertController alertController)
{
foreach (var action in alertController.Actions)
{
var lable = action.ValueForKey((NSString)"__representer");
var view = lable as UIView;
view.AccessibilityIdentifier = action.GetAccessibilityIdentifier();
}
}
}
public static class UIAlertActionExtension
{
private static readonly NSString DescriptiveName = new NSString("AccesabilityIdentifier" + nameof(UIAlertActionExtension));
#region "Extension properties"
enum AssociationPolicy
{
Assign = 0,
RetainNonAtomic = 1,
CopyNonAtomic = 3,
Retain = 01401,
Copy = 01403,
}
[DllImport("/usr/lib/libobjc.dylib")]
static extern void objc_setAssociatedObject(
IntPtr pointer, IntPtr key,
IntPtr value, AssociationPolicy policy);
[DllImport("/usr/lib/libobjc.dylib")]
static extern IntPtr objc_getAssociatedObject(
IntPtr pointer, IntPtr key);
#endregion
public static void SetAccessibilityIdentifier(this UIAlertAction alertAction, NSString accessabilityIdentifier)
{
objc_setAssociatedObject (alertAction.Handle, DescriptiveName.Handle, accessabilityIdentifier.Handle, AssociationPolicy.Retain);
}
public static NSString GetAccessibilityIdentifier(this UIAlertAction alertAction)
{
try
{
var valuePtr = objc_getAssociatedObject(alertAction.Handle, DescriptiveName.Handle); // App is crashed at this line.
if (valuePtr != IntPtr.Zero)
{
var result = ObjCRuntime.Runtime.GetNSObject(valuePtr);
if (result != null)
return (NSString)result;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
//var value = ObjCRuntime.Runtime.GetNSObject(valuePtr) as NSString;
return new NSString("");
}
}
使用上述扩展方法创建警报控制器并设置AutomationId。
private void ShowAlert()
{
var alertController = UIAlertController.Create("Title", "Message", UIAlertControllerStyle.Alert);
var action = UIAlertAction.Create("Ok", UIAlertActionStyle.Default, delegate
{
// Do Somthing
});
action.SetAccessibilityIdentifier((NSString) accessibilityIdentifier);
alertController.AddAction(action);
this.PresentViewController(alertController, true, () => { alertController.ApplyAccessibilityIdentifiers(); });
}
我们从视图控制器的按钮单击调用此方法,它可以正常工作几次。但是当第三次或第四次显示警报时,应用程序正在崩溃。
请帮我解决此问题。
提前致谢。
答案 0 :(得分:2)
AssociationPolicy.Retain
可能会在意外时间发布此属性,因此您的应用会崩溃。您可以将其更改为AssociationPolicy.RetainNonAtomic
。 NonAtomic将使您的应用程序平稳,快速地运行。
如果您只想存储字符串并在以后使用它。为什么不试试NSUserDefaults或静态实例?