【问题标题】:iPhone custom delegate problemiPhone自定义委托问题
【发布时间】:2011-04-17 11:08:27
【问题描述】:

我已经为我的类“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

【问题讨论】:

    标签: iphone ios delegation


    【解决方案1】:

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

    【讨论】:

    • 不是 nil,实际上 - 未定义,更糟糕的是,它会导致崩溃。
    • @Echelon:你错了。在对象分配时,所有实例变量都被清零。
    • 我知道我在做一些愚蠢的事情。非常感谢。
    • 是的,当然,你完全正确 - alloc 最终调用 calloc()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2010-12-29
    相关资源
    最近更新 更多