Enlazar categorías de Obj-C en MonoTouch

I am trying to bind WEPopover over to MonoTouch using btouch. It uses a UIBarButtonItem category implementation to extend it's functionality to include presentation of the popover.

@interface UIBarButtonItem(WEPopover)

- (CGRect)frameInView:(UIView *)v;
- (UIView *)superview;


How would I go about binding this into MonoTouch using a btouch interface definition?

preguntado el 28 de agosto de 11 a las 04:08

Did anyone solved this?? How to bind categories in MonoTouch?? -

2 Respuestas

My old answer missed the fact that categories were used. That was pointed out in the comments and the link to the Xamarin documentation did cover this. Quoting:

In Objective-C it is possible to extend classes with new methods, similar in spirit to C#'s extension methods. When one of these methods is present, you can use the [Target] attribute to flag the first parameter of a method as being the receiver of the Objective-C message.

For example, in MonoTouch we bound the extension methods that are defined on NSString when UIKit is imported as methods in the UIView, Así:

[BaseType (typeof (UIResponder))]
interface UIView {
    [Bind ("drawAtPoint:withFont:")]
    SizeF DrawString ([Target] string str, PointF point, UIFont font);

De: http://docs.xamarin.com/ios/advanced_topics/binding_objective-c_types#Binding_Class_Extensions

The above example is what MonoTouch uses to bind drawAtPoint:withFont: que es parte de la Adiciones de NSString UIKit

Respondido 06 ago 12, 19:08

Could you elaborate a bit more?? since I don't have the UIKit .h files to see how was the objc code, maybe the binding code from the original question please :) - dalexsoto

The header files comes with Xcode and without it you would not be able to use MonoTouch. The documentation for drawAtPoint:withFont: (link included in answer) tells you to look for UIStringDrawing.h then you can find it using: find /Applications/Xcode.app/ -name UIStringDrawing.h then open it using (something like) open /Applications/Xcode.app//Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIStringDrawing.h. note: I won't copy/paste it since the file is copyrighted by Apple - poupou

Here's a quick (and untested) definition for the above:

using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Drawing;

namespace MonoTouch.Popover {
    [BaseType (typeof (UIBarButtonItem))]
    public interface WEPopover {
        [Export ("frameInView")]
        RectangleF FrameInView (UIView view);

        [Export ("superView")]
        UIView SuperView { get; }

Entonces usas el btouch tool to compile the definition into a .dll usable from MonoTouch, e.g.

imac:tmp sebastienpouliot$ /Developer/MonoTouch/usr/bin/btouch we.cs 
imac:tmp sebastienpouliot$ ls -l we.dll
-rwxr-xr-x  1 sebastienpouliot  staff  5632 28 Aug 10:23 we.dll

I suggest you to read the existing documentation on how to bind existing ObjectiveC libraries. The document is available from: http://ios.xamarin.com/Documentation/Binding_New_Objective-C_Types

Respondido 28 ago 11, 18:08

The problem is that those two members are actually parte of UIBarButtonItem since obj-C category members are incorporated into the underlying type. So I don't think standard binding will work, at least it's not documented that I could see. - Anuj

As stated, this wont work at all. What you need to do is hand bind it and add extension methods to UIBarButtonItem. - Geoff Norton

Has anyone found a solution for this yet? - rogchap

Did anyone solved this?? How to bind categories in MonoTouch?? Any example on how to manually bind it? - dalexsoto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.