Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general, <a href="https://stackoverflow.com/a/11655492/63225">@Monoman's <code>is</code> solution</a> is the right way to find instances of a particular class, even when you're looking for a CocoaTouch class from within a MonoTouch program.</p> <p>Sometimes, however, you'll discover that there's an internal CocoaTouch class that's not exposed in MonoTouch (or even in the iOS Platform headers). In such a case, you'll have to resort to <a href="https://stackoverflow.com/a/11657997/63225">tricks like @poupou is doing</a>.</p> <p>Unfortunately, his answers won't work here <em>either</em>. <code>view.GetType()</code> is returning the most-derived MonoTouch type that each <code>Subview</code> implements, and then <code>ToString()</code>, <code>Class.Name</code>, or even <code>@selector("description")</code> each operate on the wrong type and give an overly generic answer ("<code>UIView</code>" in this case).</p> <p>In order to make this work, you'll have to go one layer <em>deeper</em> under the covers than @poupou proposed.</p> <pre><code>// ** 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; } </code></pre> <p>Here's an alternative that's not a whole lot more fiddly (maybe even less?), but will work on any Objective-C class, not just those that respond to <code>NSObject</code>'s <code>description</code> message:</p> <pre><code>// ** 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)); } </code></pre> <p>It's actually surprising and a little bit sad that MonoTouch doesn't provide an import for <code>object_getClassName()</code> already.</p> <p>You would use either one of those like so:</p> <pre><code>foreach (UIView view in cell.Subviews) { if (GetClassName(view.Handle) == "UITableViewCellReorderControl") { } } </code></pre> <p><strong>Big fat disclaimer</strong>: Pretty much any time you're resorting to tricks like these, you're relying on CocoaTouch implementation details that Apple reserves the right to change. Rule of thumb: if you can do what you want with @Monoman's solution, you're <em>probably</em> safe. Otherwise, you're taking matters in to your own hands.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload