ios Objective C 委托方法
ios Objective C delegate methods
这是我Objective C的第一天,所以对于知识的不足,我深表歉意。
我需要将现有的 SKD 导入应用程序,我成功地完成了。现在我需要创建委托方法,但我不知道该怎么做。
这是 SDK (SDKManager.h) 中包含的 header 文件的结构:
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
@property (nonatomic, weak) id<SDKDelegate> delegate;
+(void)initialize:(NSString*)appId withKEY:(NSString*)key;
+(void)setHandler:(id)delegate;
@end
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items withResponse:(NSDictionary*)response;
@end
所以,从我的 FirstViewController.m 我能够导入 header 并调用两个方法:
#import "FirstViewController.h"
#import "SDKManager.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[SDKManager setHandler:[UIApplication sharedApplication].delegate];
[SDKManager initialize:@"AppId"withKEY:@"1234"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
但我注意到我无法调用其他方法(即 appDidReceiveTokens)。
实际上说明需要创建这些方法,但我不知道在哪里。
非常感谢任何帮助。
谢谢
您不直接在要实现委托方法的文件中调用委托方法。回顾Apples documentation on the concept of Delegation。
要正确实现这一点,您可以在 class 中采用委托,然后实现 @required
and/or @optional
.
的委托方法
您已正确创建委托协议和 属性 来存储 SDKManager
的委托。
您的 setHandler:
和 initialize:withKEY:
方法是 class 方法,而 delegate
属性 属于SDKManager
的每个 实例 。没有看到 SDKManager
的实现文件 (.m),很难知道为什么要这样设置它。您可能正在尝试遵循单例模式 - 如果是这样,请仔细阅读,例如here.
原因是您有 class 方法来设置调用 setHandler 方法,委托是 属性,所以您在哪里分配委托以及何时以及如何调用委托.我希望您了解 class 和实例是什么。因此,在创建对象实例之前不能调用委托。
您有两种不同的 class 方法,用于将某些属性设置为 class,将它们设置为 属性 是否有意义。
更通用和更好的方法是这样的,
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items
withResponse:(NSDictionary*)response;
@end
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate;
@end
@interface SDKManager ()
@property (nonatomic, copy, readonly) NSString *appId;
@property (nonatomic, copy, readonly) NSString *key;
@property (nonatomic, weak, readonly) id<SDKManagerDelegate> delegate;
@end
@implementation SDKManager
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate
{
if (self = [super init]) {
_appId = [appId copy];
_key = [key copy];
_delegate = delegate;
}
return self;
}
- (void)doSomeNetworkRequestHere
{
[self fetchTokenFromServer:^(NSDictionary *tokens, NSDictionary *response){
[self.delegate appDidReceiveTokens:tokens
withResponse:response];
}];
}
- (void)fetchTokenFromServer:(void(^)(NSDictionary *tokens, NSDictionary *response))completion
{
}
@end
这是我Objective C的第一天,所以对于知识的不足,我深表歉意。
我需要将现有的 SKD 导入应用程序,我成功地完成了。现在我需要创建委托方法,但我不知道该怎么做。
这是 SDK (SDKManager.h) 中包含的 header 文件的结构:
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
@property (nonatomic, weak) id<SDKDelegate> delegate;
+(void)initialize:(NSString*)appId withKEY:(NSString*)key;
+(void)setHandler:(id)delegate;
@end
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items withResponse:(NSDictionary*)response;
@end
所以,从我的 FirstViewController.m 我能够导入 header 并调用两个方法:
#import "FirstViewController.h"
#import "SDKManager.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[SDKManager setHandler:[UIApplication sharedApplication].delegate];
[SDKManager initialize:@"AppId"withKEY:@"1234"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
但我注意到我无法调用其他方法(即 appDidReceiveTokens)。 实际上说明需要创建这些方法,但我不知道在哪里。
非常感谢任何帮助。 谢谢
您不直接在要实现委托方法的文件中调用委托方法。回顾Apples documentation on the concept of Delegation。
要正确实现这一点,您可以在 class 中采用委托,然后实现 @required
and/or @optional
.
您已正确创建委托协议和 属性 来存储 SDKManager
的委托。
您的 setHandler:
和 initialize:withKEY:
方法是 class 方法,而 delegate
属性 属于SDKManager
的每个 实例 。没有看到 SDKManager
的实现文件 (.m),很难知道为什么要这样设置它。您可能正在尝试遵循单例模式 - 如果是这样,请仔细阅读,例如here.
原因是您有 class 方法来设置调用 setHandler 方法,委托是 属性,所以您在哪里分配委托以及何时以及如何调用委托.我希望您了解 class 和实例是什么。因此,在创建对象实例之前不能调用委托。
您有两种不同的 class 方法,用于将某些属性设置为 class,将它们设置为 属性 是否有意义。
更通用和更好的方法是这样的,
@protocol SDKManagerDelegate <NSObject>
@required
-(void)appDidReceiveTokens:(NSDictionary*)items
withResponse:(NSDictionary*)response;
@end
@protocol SDKManagerDelegate;
@interface SDKManager : NSObject
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate;
@end
@interface SDKManager ()
@property (nonatomic, copy, readonly) NSString *appId;
@property (nonatomic, copy, readonly) NSString *key;
@property (nonatomic, weak, readonly) id<SDKManagerDelegate> delegate;
@end
@implementation SDKManager
- (instancetype)initWithAppId:(NSString *)appId
key:(NSString *)key
delegate:(id<SDKManagerDelegate>)delegate
{
if (self = [super init]) {
_appId = [appId copy];
_key = [key copy];
_delegate = delegate;
}
return self;
}
- (void)doSomeNetworkRequestHere
{
[self fetchTokenFromServer:^(NSDictionary *tokens, NSDictionary *response){
[self.delegate appDidReceiveTokens:tokens
withResponse:response];
}];
}
- (void)fetchTokenFromServer:(void(^)(NSDictionary *tokens, NSDictionary *response))completion
{
}
@end