Getting the Objective-C Class Name Without Using the MonoTouch Managed Class

I can’t understand for my whole life how to translate this into MonoTouch. Any members?

for(UIView* view in cell.subviews) { if([[[view class] description] isEqualToString:@"UITableViewCellReorderControl"]) { } } 

Basically this is a class class description that puzzled me ...

+6
source share
4 answers

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.

+7
source

I am not currently using Monotouch, but shouldn't you just check the managed type of the view?

  foreach (UIView view in cell.subviews) { if (view is UITableViewCellReorderControl) {} } 
+5
source

Objective-C Class handling is mostly hidden in the API provided by MonoTouch. This is mainly System.Type and description is basically ToString - but this will not show some internal elements.

Here are a few things (simple and complex) you can try:

A call to ToString in a view instance may include the original class name (as part of the description) - but this is not the same as calling the description the Class itself.

  foreach (UIView view in cell.Subviews) { if (view.ToString ().Contains ("UITableViewCellReorderControl")) { } } 

Then try to see if Name matches the Class description .

 foreach (UIView view in cell.Subviews) { MonoTouch.ObjCRuntime.Class c = new MonoTouch.ObjCRuntime.Class (view.GetType ()); if (c.Name == "UITableViewCellReorderControl") { } } 

Finally, really call the description selector on the Class instance. Something like (untied):

 MonoTouch.ObjCRuntime.Selector description = new MonoTouch.ObjCRuntime.Selector ("description"); foreach (UIView view in cell.Subviews) { MonoTouch.ObjCRuntime.Class c = new MonoTouch.ObjCRuntime.Class (view.GetType ()); var name = NSString.FromHandle (MonoTouch.ObjCRuntime.Messaging.IntPtr_objc_msgSend (c.Handle, description.Handle)); if (name == "UITableViewCellReorderControl") { } } 
+5
source

This works for me:

 foreach(var subview in this.ContentView.Superview.Subviews) { if(subview.Class.Name.EndsWith("UITableViewCellReorderControl")) { } } 
-1
source

Source: https://habr.com/ru/post/921328/


All Articles