In general, the @Monoman is
solution is
the right way to search for instances of a particular class, even if you are looking for the CocoaTouch class from within the MonoTouch program.
Sometimes, however, you will find that there is an internal CocoaTouch class that does not appear in MonoTouch (or even in iOS platform headers). In this case, you have to resort to tricks, for example @poupou .
Unfortunately, his answers here will not work either. view.GetType()
returns the most derived MonoTouch type that each Subview
, and then ToString()
, Class.Name
or even @selector("description")
work out of type and give too general an answer (" UIView
" in this case )
To make this work, you will need to go one layer deeper under the covers than @poupou suggested.
// ** using MonoTouch.ObjCRuntime; ** private string GetClassName (IntPtr obj) { Selector description = new Selector ("description"); Selector cls = new Selector ("class"); IntPtr viewcls = Messaging.IntPtr_objc_msgSend (obj, cls.Handle); var name = NSString.FromHandle (Messaging.IntPtr_objc_msgSend (viewcls, description.Handle)); return name; }
Here is an alternative that is not much more complicated (maybe even less?), But will work in any Objective-C class, and not only those that respond to the NSObject
description
message:
// ** using System.Runtime.InteropServices; ** [DllImport ("/usr/lib/libobjc.dylib")] private static extern IntPtr object_getClassName (IntPtr obj); private string GetClassName (IntPtr obj) { return Marshal.PtrToStringAuto(object_getClassName(obj)); }
It is actually surprising and a little sad that MonoTouch no longer provides imports for object_getClassName()
.
You would use one of these:
foreach (UIView view in cell.Subviews) { if (GetClassName(view.Handle) == "UITableViewCellReorderControl") { } }
Big rejection of fat . Almost at any time when you resort to such tricks, you rely on the CocoaTouch implementation details that Apple reserves the right to change. Rule of thumb: if you can do what you want with the @Monoman solution, you are likely to be safe. Otherwise, you solve your problems.