Binding Categories Obj-C in MonoTouch

I am trying to pair WEPopover with MonoTouch using btouch. It uses an implementation of the UIBarButtonItem category to extend its functionality by including a popover presentation.

 @interface UIBarButtonItem(WEPopover) - (CGRect)frameInView:(UIView *)v; - (UIView *)superview; @end 

How can I associate this with MonoTouch using the btouch interface definition?

+8
objective-c
source share
2 answers

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

In Objective-C, you can extend classes with new methods that are similar in spirit to C # extension methods. When one of these methods, you can use the [Target] attribute to get the first parameter of the method as the receiver of the Objective-C message.

For example, in MonoTouch, we linked extension methods that are defined on NSString when UIKit is imported as methods in UIView , for example:

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

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

The above example is what MonoTouch uses to drawAtPoint:withFont: which is part of the NSString UIKit Additions

+6
source share

Here is a quick (and unverified) 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; } } } 

Then you use the btouch tool to compile the definition into a .dll, which you can use in MonoTouch, for example.

 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 familiarize yourself with existing documentation on how to link existing ObjectiveC libraries. The document is available at: http://ios.xamarin.com/Documentation/Binding_New_Objective-C_Types

0
source share

All Articles