Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding Obj-C Categories in 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;

@end

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

like image 683
Anuj Avatar asked Aug 28 '11 04:08

Anuj


1 Answers

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, like this:

[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 bind drawAtPoint:withFont: which is part of the NSString UIKit Additions

like image 181
poupou Avatar answered Nov 10 '22 04:11

poupou