2
votes

How do you define parameters/returntypes in MonoTouch Bindings of Objective-c parameters that are "types implementing protocol", for example "UIView<NIRecyclableView>".

For example, check this header file.

https://github.com/jverkoey/nimbus/blob/master/src/core/src/NIViewRecycler.h

@interface NIViewRecycler : NSObject
- (UIView<NIRecyclableView> *)dequeueReusableViewWithIdentifier:(NSString *)reuseIdentifier;
- (void)recycleView:(UIView<NIRecyclableView> *)view;
- (void)removeAllViews;
@end

Should I just have the parameters declared as IntPtr? If so, how do I convert a MonoTouch instance to a IntPtr?

1

1 Answers

1
votes

In the same header file you have a NIRecyclableView class that it inherits from UIView and implements NIRecyclableView protocol so you might want to return that object instead

@interface NIRecyclableView : UIView <NIRecyclableView>

// Designated initializer.
- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier;

@property (nonatomic, readwrite, copy) NSString* reuseIdentifier;

@end

So the binding of that header would look like this

[BaseType (typeof (NSObject))]
interface NIViewRecycler {
    [Export ("dequeueReusableViewWithIdentifier:")]
    NIRecyclableView DequeueReusableView (string reuseIdentifier);

    [Export ("recycleView:")]
    void RecycleView (NIRecyclableView view);

    [Export ("removeAllViews")]
    void RemoveAllViews ();
}

[Model]
[Protocol]
[BaseType (typeof (NSObject), Name = "NIRecyclableView")]
interface NIRecyclableViewProtocol {
    [Export ("reuseIdentifier", ArgumentSemantic.Copy)]
    string ReuseIdentifier { get; set; }

    [Export ("prepareForReuse")]
    void PrepareForReuse ();
}

// Here you would do interface NIRecyclableView : NIRecyclableViewProtocol
// But NIRecyclableView already implements reuseIdentifier
// So you just inline the missing method PrepareForReuse
// and you get the same results
[BaseType (typeof (UIView))]
interface NIRecyclableView {
    [Export ("initWithReuseIdentifier:")]
    IntPtr Contructor (string reuseIdentifier);

    [Export ("reuseIdentifier", ArgumentSemantic.Copy)]
    string ReuseIdentifier { get; set; }

    [Export ("prepareForReuse")]
    void PrepareForReuse ();
}

A protocol is like hey this object also can respond to this objective-c messages if you want, so you can either manually inline them on the interface definition like we did above or NIRecyclableView : NIRecyclableViewProtocol which in this case we don't have to.

Hope this helps

Alex