iPhone自定义委托问题

Jac*_*ack 0 iphone delegation ios

我为我的班级'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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

WelcomeViewController.h

#import <UIKit/UIKit.h>
#import "HotRequest.h"

@interface WelcomeViewController : UIViewController <HotRequestDelegate>{
     HotRequest *myRequest;
}

@property (nonatomic,retain) HotRequest *myRequest;

@end
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

Ole*_*ann 5

delegate仍然nilinitWithRequestOptions:.您正在尝试在设置委托之前调用委托方法.