Skip to content

Instantly share code, notes, and snippets.

@michaelstonis
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelstonis/1d86e9170a1ea871fe96 to your computer and use it in GitHub Desktop.
Save michaelstonis/1d86e9170a1ea871fe96 to your computer and use it in GitHub Desktop.
Xamarin.iOS Control ID Generator
public static void GenerateViewNames(this UIViewController viewController, bool forceExecution = false){
if(!forceExecution
&&
(
ObjCRuntime.Runtime.Arch != ObjCRuntime.Arch.SIMULATOR
||
!UIDevice.CurrentDevice.CheckSystemVersion(8, 0)
))
return;
if (viewController.NavigationController != null)
viewController.NavigationController.GenerateViewNames (forceExecution);
if (viewController.TabBarController != null)
viewController.TabBarController.GenerateViewNames (forceExecution);
var stopwatch = System.Diagnostics.Stopwatch.StartNew ();
var viewControllerType = viewController.GetType ();
System.Diagnostics.Debug.WriteLine (string.Format("\t----- Missing View IDs for {0} -----", viewControllerType.Name));
System.Diagnostics.Debug.WriteLine ("\t----- Generated IDs for Fields -----");
viewControllerType.GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField)
.ToList()
.ForEach (field => {
try {
var uiView = field.GetValue(viewController) as UIView;
if(uiView != null){
if(string.IsNullOrEmpty(uiView.AccessibilityIdentifier)){
uiView.AccessibilityIdentifier = field.Name;
System.Diagnostics.Debug.WriteLine(string.Format("\t{0}.AccessibilityIdentifier = \"{0}\";", field.Name));
}
}
} catch (ArgumentException) {
System.Diagnostics.Debug.WriteLine(string.Format("\tUnable to Bind {0}", field.Name));
}
});
System.Diagnostics.Debug.WriteLine ("\t----- Generated IDs for Properties -----");
viewControllerType.GetProperties (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty)
.ToList()
.ForEach (property => {
try {
var uiView = property.GetValue(viewController) as UIView;
if(uiView != null){
if(string.IsNullOrEmpty(uiView.AccessibilityIdentifier)){
uiView.AccessibilityIdentifier = property.Name;
System.Diagnostics.Debug.WriteLine(string.Format("\t{0}.AccessibilityIdentifier = \"{0}\";", property.Name));
}
}
} catch (ArgumentException) {
System.Diagnostics.Debug.WriteLine(string.Format("\tUnable to Bind {0}", property.Name));
}
});
stopwatch.Stop ();
System.Diagnostics.Debug.WriteLine (string.Format("----- Processing Time for {0} : {1}ms -----", viewControllerType.Name, stopwatch.ElapsedMilliseconds));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment