Delegate为NULL

时间:2014-08-28 08:24:57

标签: ios objective-c delegates

我有一个表格视图,我从哪里发送请求来自网址的数据。我能够获取数据,但我想使用委托将数据发送回表视图。

问题是,当我指定 self.delegate 时,它包含一个值,但当我尝试发回数据时, self.delegate 变为0x000000。

在viewcontroller.m

GNRequestManager *request = [[GNRequestManager alloc]initWithDelegate:self];

在GNRequestManager.h中

#import <Foundation/Foundation.h>
#import "GNDownloadManager.h"

@protocol RequestManagerDelegate <NSObject>

- (void)didReceiveData:(NSMutableArray *)data;

@end


@interface GNRequestManager : NSObject

@property (nonatomic, weak) id<RequestManagerDelegate> delegate;

-(id)initWithDelegate:(id<RequestManagerDelegate>)inDelegate;

- (void)requestWithURL:(NSString *)urlString;

-(void)didReceiveResponse:(NSMutableArray *)downloadData;



@end

在GNRequestManager.m

-(id)initWithDelegate:(id<RequestManagerDelegate>)inDelegate
{
    self = [super init];

    if (self)
    {
        self.delegate = inDelegate; // **self.delegate has a value**

    }

    return self;
}



- (void)didReceiveResponse:(NSMutableArray *)downloadData // Fetch the data in downloadData

{

    if(!itemArray)
    {
        itemArray = [[NSMutableArray alloc]init];
    }

    itemArray = downloadData;

    [self.delegate didReceiveData:itemArray]; // **self.delegate does not contain any value**

}

1 个答案:

答案 0 :(得分:1)

我曾经有过这个问题。据我所知,您将delegate存储为具有weak修饰符的属性。这意味着您的GNRequestManager不会增加您的委托对象的引用计数。 如果要创建委托对象,请执行以下操作:

GNDelegateObject *delegate = [GNDelegateObject new];
GNRequestManager *manager = [[GNRequestManager alloc] initWithDelegate:delegate];

委托对象将被释放。

您应该在其他地方(而不是GNRequestManager)对您的委托对象进行强烈引用。

UPD:您已经更新了问题,代理设置如下:

GNRequestManager *request = [[GNRequestManager alloc]initWithDelegate:self];

其中selfUIViewController子类的实例。

此视图控制器是否仍在视图控制器层次结构中?也许,你已经解雇/弹出它以便它现在被解除分配?