iPhone自定义委托问题

时间:2010-09-26 16:58:24

标签: iphone ios delegation

我为我的班级'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

1 个答案:

答案 0 :(得分:5)

delegate中{p> nil仍为initWithRequestOptions:。您正在尝试在设置委托之前调用委托方法。