使AppDelegate从自定义库接收回调事件

时间:2014-04-10 02:29:51

标签: ios objective-c static-libraries appdelegate

我正在尝试实现一个库,但是该库带有最少的文档,而且我有点超出我的深度。通过查看其他代码我到目前为止

文件说 AppDelegate将JBLayer的对象创建为成员变量

所以我在AppDelegate.h中做到了这一点

#import "JBLayer.h"
//@class JBLayer;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    JBLayer *aJBLayer;
}
@property (nonatomic, strong, readonly) JBLayer *aJBLayer;

这在AppDelegate.m

@implementation AppDelegate
@synthesize aJBLayer = _aJBLayer;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

    // THIS IS WHERE I'm STUCK
    [aJBLayer SetHandler:self.doSetHandler];

    // This is what I have to include the lib for, this seems to be ok
    [aJBLayer MainCall];

    return YES;
}

-(id)doSetHandler
{
    return WHAT???;
}

文件说 通过调用SetHandler将处理程序设置为接收回调事件。如果AppDelegate想要接收回叫事件,AppDelegate需要传递self。

查看JBLayer.h文件,我发现:

-(void)SetHandler:(id<interfaceJBHandler>)aHandler;

我必须实现interfaceJBHandler.h中的这些interfaceJBHandler方法

@protocol interfaceJBHandler
-(void)OnJBSuccess:(NSString *)aToken;
-(void)OnJBError:(NSError *)aError;
-(void)OnJBResponse :(NSData *)aData :(JBType )aTypeOfData;

我得到了类似

的东西
[aJBLayer SetHandler:];

但我尝试过的所有内容都会产生错误

[aJBLayer SetHandler:self];
[aJBLayer SetHandler:appDelegate];

这个编译

[aJBLayer SetHandler:self.doSetHandler];

但是我错过了一些东西,某个地方我必须成为一名代表,但到目前为止我只是委托代表在viewcontrollers之间传递消息。

如何让AppDelegate接收回叫事件?

1 个答案:

答案 0 :(得分:3)

您需要传递实现interfaceJBHandler协议的对象。如果您希望appDelegate成为对象,请在头文件中更改以下内容,以使您的app委托符合协议:

@interface AppDelegate : UIResponder <UIApplicationDelegate, interfaceJBHandler>

将委托设置为处理程序:

[aJBLayer SetHandler:self];

在app delegate的实现文件中实现3个必需的方法:

-(void)OnJBSuccess:(NSString *)aToken{
    //Do Something
}

-(void)OnJBError:(NSError *)aError{
    //Do Something
}

-(void)OnJBResponse :(NSData *)aData :(JBType )aTypeOfData{
    //Do Something
}