开发者

iPhone custom delegate problem

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)requestComplet开发者_JS百科e {
    NSLog(@"request complete");
}
@end


delegate is still nil in initWithRequestOptions:. You are trying to call the delegate method before setting the delegate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜