iPhone自定义委托问题
我已经为我的类“HotRequest”设置了一个委托,但在实现它时遇到了问题。我的班级的代码如下。有什么想法吗?谢谢
HotRequest.h
#import <Foundation/Foundation.h>
@protocol HotRequestDelegate;
@interface HotRequest : NSObject {
NSString *requestString;
id <HotRequestDelegate> delegate;
}
@property (nonatomic, retain) NSString *requestString;
@property (nonatomic, assign) id <HotRequestDelegate> delegate;
- (id)initWithRequestOptions:(NSDictionary*)dict;
@end
@protocol HotRequestDelegate <NSObject>
@required
- (void)requestComplete;
@end
HotRequest.m
#import "HotRequest.h"
@implementation HotRequest
@synthesize requestString, delegate;
- (id)initWithRequestOptions:(NSDictionary*)dict {
if ((self = [super init])) {
for (NSString *key in [dict allKeys]) {
requestString = [NSString stringWithFormat:@"%@&%@=%@", requestString, key, [dict objectForKey:key]];
}
NSLog(@"%@", requestString);
}
[delegate requestComplete];
return self;
}
@end
WelcomeViewController.h
#import <UIKit/UIKit.h>
#import "HotRequest.h"
@interface WelcomeViewController : UIViewController <HotRequestDelegate>{
HotRequest *myRequest;
}
@property (nonatomic,retain) HotRequest *myRequest;
@end
WelcomeViewController.m
#import "WelcomeViewController.h"
#import "HotRequest.h"
@implementation WelcomeViewController
@synthesize myRequest;
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *mydict = [[NSDictionary alloc] initWithObjectsAndKeys:@"2", @"1", @"4", @"3", nil];
myRequest = [[HotRequest alloc] initWithRequestOptions:mydict];
[[self myRequest] setDelegate:self];
}
- (void)requestComplete {
NSLog(@"request complete");
}
@end
I have set up a delegate for my class 'HotRequest', but am having problems implementing it. The code for my class is below. Any ideas? Thanks
HotRequest.h
#import <Foundation/Foundation.h>
@protocol HotRequestDelegate;
@interface HotRequest : NSObject {
NSString *requestString;
id <HotRequestDelegate> delegate;
}
@property (nonatomic, retain) NSString *requestString;
@property (nonatomic, assign) id <HotRequestDelegate> delegate;
- (id)initWithRequestOptions:(NSDictionary*)dict;
@end
@protocol HotRequestDelegate <NSObject>
@required
- (void)requestComplete;
@end
HotRequest.m
#import "HotRequest.h"
@implementation HotRequest
@synthesize requestString, delegate;
- (id)initWithRequestOptions:(NSDictionary*)dict {
if ((self = [super init])) {
for (NSString *key in [dict allKeys]) {
requestString = [NSString stringWithFormat:@"%@&%@=%@", requestString, key, [dict objectForKey:key]];
}
NSLog(@"%@", requestString);
}
[delegate requestComplete];
return self;
}
@end
WelcomeViewController.h
#import <UIKit/UIKit.h>
#import "HotRequest.h"
@interface WelcomeViewController : UIViewController <HotRequestDelegate>{
HotRequest *myRequest;
}
@property (nonatomic,retain) HotRequest *myRequest;
@end
WelcomeViewController.m
#import "WelcomeViewController.h"
#import "HotRequest.h"
@implementation WelcomeViewController
@synthesize myRequest;
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *mydict = [[NSDictionary alloc] initWithObjectsAndKeys:@"2", @"1", @"4", @"3", nil];
myRequest = [[HotRequest alloc] initWithRequestOptions:mydict];
[[self myRequest] setDelegate:self];
}
- (void)requestComplete {
NSLog(@"request complete");
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
initWithRequestOptions:
中,delegate
仍然是nil
。您尝试在设置委托之前调用委托方法。delegate
is stillnil
ininitWithRequestOptions:
. You are trying to call the delegate method before setting the delegate.