我正在尝试将TapForTap iOS SDK移植到我的Monotouch C#应用程序中。

使用文档和绑定生成器,我创建了以下绑定ApiDefintion:

namespace TapForTap {

[BaseType (typeof (NSObject))]
interface TapForTap {
    [...]
    [Static]
    [Export ("initializeWithAPIKey:")]
    void InitializeWithAPIKey (string key);
}

[BaseType (typeof (NSObject))]
[Model]
interface TapForTapAdViewDelegate {
    [Abstract]
    [Export ("rootViewController")]
    UIViewController RootViewController ();

    [...]
}

[BaseType (typeof (UIView))]
interface TapForTapAdView {
    [Static]
    [Export ("initWithFrame:delegate:")]
    NSObject Init (RectangleF frame, TapForTapAdViewDelegate d);

    [Export ("loadAds")]
    void LoadAds ();

    [Export ("stopLoadingAds")]
    void StopLoadingAds ();

            [...]
}
}


在我的项目中,我创建了一个实现TapForTapAdViewDelegate的类TapForTapDelegate。

public partial class TapForTapAdDelegate : TapForTapAdViewDelegate {
    public override UIViewController RootViewController () {
        return Application.mainViewController;
    }

    [...]
}


问题是我真的不确定我是否正确实现了initWithFrame:delegate:函数。

每当我尝试使用以下代码时,我都会得到NSInvalidArgumentException Reason: +[TapForTapAdView initWithFrame:delegate:]: unrecognized selector sent to class 0x36dac4

bannerAd = (TapForTapAdView) TapForTapAdView.Init (new System.Drawing.RectangleF(0, y, 320, 50), new TapForTapAdDelegate());


我究竟做错了什么?我已经尝试了几个小时,但没有找到任何解决方案。
我已经习惯了Java,而不是C#和Objective-C;)

最佳答案

问题是init *方法绑定为C#构造函数。

尝试这个:

[BaseType (typeof (UIView))]
interface TapForTapAdView {
    [Export ("initWithFrame:delegate:")]
    IntPtr Constructor (RectangleF frame, TapForTapAdViewDelegate d);

    ...
}


Here是有关如何在Xamarin.iOS中绑定Objective-C库的更多文档。特别是请参见3.3 Binding Constructors部分。

关于c# - 在Xamarin中绑定(bind)iOS委托(delegate)时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15530494/

10-11 02:05
查看更多