gpt4 book ai didi

iPhone自定义委托(delegate)问题

转载 作者:行者123 更新时间:2023-11-28 18:45:37 25 4
gpt4 key购买 nike

我已经为我的类“HotRequest”设置了一个委托(delegate),但是在实现它时遇到了问题。我类的代码如下。有任何想法吗?谢谢

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

最佳答案

delegateinitWithRequestOptions: 中仍然是 nil。您正在尝试在设置委托(delegate)之前调用委托(delegate)方法。

关于iPhone自定义委托(delegate)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3798843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com