gpt4 book ai didi

objective-c - 绑定(bind)方法与委托(delegate)

转载 作者:太空狗 更新时间:2023-10-30 03:52:26 24 4
gpt4 key购买 nike

我正在创建一个绑定(bind)库来绑定(bind) native Objective-C 框架。

我有以下委托(delegate),我需要将它的声明添加到 ApiDefinition 文件,然后我需要使用我的 Xamarin.iOS 应用程序实现它:

- (void)Initialize:(id <MMSDKDelegate> _Nullable)pDelegate;

MMSDKDelegate:

@protocol MMSDKDelegate <IMMDelegate>
- (void)Started;
@end

IMMDelegate:

@protocol IMMDelegate
- (void)Inserted;
- (void)Removed;
- (void)ReaderConnected;
- (void)ReaderRemoved;
@end

我需要 ApiDefinition 文件中的所需定义,并且我需要一个示例代码来从我的 Xamarin.iOS 应用程序调用此方法。

更新

我正在处理的框架正在与 card reader device 通信附在iPhone上读取身份证信息,有读卡器插入/取出和卡插入/取出时调用的方法..

我已经实现了@cole-xia 的答案,但问题是当我插入读卡器或 ID 时,永远不会调用 IMMDelegate 中的方法。当我调用 ReadCardData() 时,它应该调用 Started() 来显示 Inserted() 保存的信息,但当前的结果是Started() 方法在调用 ReadCardData() 之后被调用,但是 Inserted()ReaderConnected() 永远不会在任何阶段调用。

在Framework的demo app中,如下使用(并正常运行):

// Demo app -> ViewController.m

@interface ViewController () <MMSDKDelegate>

@end


@implementation ViewController {
MMSDK *sdkInstance;
}


- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

sdkInstance = [MMSDK SharedInstance];
[sdkInstance Initialize:self];
}


- (void)Started
{
// Update UI by: reading in progress ..
}


- (void)Inserted
{
// Update UI by: card inserted
// And read card data
[self readData:self];
}

- (void)Removed
{
// Update UI by: card removed
}

- (void)ReaderConnected
{
// Update UI by: card reader connected
}

- (void)ReaderRemoved
{
// Update UI by: card reader removed
}


- (IBAction)readData:(id)sender
{
var *data = [sdkInstance ReadCardData:YES pWithSignatureImage:YES pWithAddressData:YES];
if (data.hasError) {
// No data fetched
return;
}

if (data) {
// return card data
}
}

欢迎并赞赏所有建议。

总而言之,我只需要在 Xamarin.iOS 应用中执行与演示应用相同的功能。

最佳答案

使用 Sharpie 创建 ApiDefinition

我这边的结果:

// @protocol IMMDelegate
[BaseType (typeof (NSObject))]
[Protocol, Model]
interface IMMDelegate
{
// @required -(void)Inserted;
[Abstract]
[Export ("Inserted")]
void Inserted ();

// @required -(void)Removed;
[Abstract]
[Export ("Removed")]
void Removed ();
}

// @protocol MMSDKDelegate <IMMDelegate>
[BaseType (typeof (NSObject))]
[Protocol, Model]
interface MMSDKDelegate : IMMDelegate
{
// @required -(void)Started;
[Abstract]
[Export ("Started")]
void Started ();
}

// @interface ACR : NSObject
[BaseType (typeof(NSObject))]
interface YourClass
{
// -(void)Initialize:(id<MMSDKDelegate> _Nullable)pDelegate;
[Export ("Initialize:")]
void Initialize ([NullAllowed] MMSDKDelegate pDelegate);
}

用法:

class MyDelegate : MMSDKDelegate {
public void Started()
{
}

public override void Removed()
{
}
public override void Inserted()
{
}
}


//In ViewController

public override void ViewDidLoad()
{
base.ViewDidLoad();

YourClass yourClass = new YourClass();
yourClass.Initialize(new MyDelegate());
}

关于objective-c - 绑定(bind)方法与委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com