自定义委托方法不会被调用

时间:2014-12-09 10:21:55

标签: ios objective-c

所以,我在尝试在没有故事板的iOS项目中的两个类之间传递数据时遵循this的答案。我正在尝试在不同的类中抽象我的所有服务器调用,因此ViewControllers不会太过局促。我有一个OfferRequest类,它处理服务器请求并声明协议:

@protocol OfferRequestDelegate <NSObject>

-(void)addOfferRequest: (OfferRequest *)request dataFromRequest: (NSMutableArray *)data;

@end

@interface OfferRequest : NSObject <LocationManagerDelegate, NSURLConnectionDataDelegate>

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

我们的想法是在addOfferRequest: dataFromRequest中调用connectionDidFinishLoading,将数据设置为请求的结果并将其传递给ViewController。

在OfferRequest.m中:

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:nil];
    [self.delegate addOfferRequest:self dataFromRequest:result];

}

我声明我的ViewController是OfferRequest的委托:

@interface CategorySuggestionViewController : UIViewController <OfferRequestDelegate>

初始化OfferRequest对象并设置它的委托:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    offer = [OfferRequest new];
    offer.delegate = self;
}

然后实现委托方法:

-(void)addOfferRequest:(OfferRequest *)request dataFromRequest:(NSMutableArray *)data {
    NSLog(@"received data = %@", data);
}

但它永远不会被召唤。我错过了什么?

3 个答案:

答案 0 :(得分:0)

试试这个......

您的OfferRequest类如下所示,我已将其命名为connectionClass

connectionClass.h

@protocol connectionDelegate <NSObject>
-(void)connectionSucceed:(NSData *)data;
-(void)connectionFailed:(NSError *)error;
@end

#import <Foundation/Foundation.h>

@interface connectionClass : NSObject
{
    id<connectionDelegate> delegate;
    NSMutableData *receivedData;
    NSString *encodedString;
    NSMutableURLRequest *theRequest;
    NSURL *url;
    NSString *length;
    NSURLConnection *Connect;

}
@property(nonatomic, retain) id<connectionDelegate> delegate;
@property(nonatomic, retain) NSMutableData *receivedData;
- (void)connectionWithURL:(NSString *)URL;
@end

<强> connectionClass.m

#import "connectionClass.h"

@implementation connectionClass
@synthesize delegate, receivedData;
- (void)connectionWithURL:(NSString *)URL
{
    url=[[NSURL alloc] initWithString:[self urlencode:URL]];
    theRequest= [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    length = [NSString stringWithFormat:@"%lu", (unsigned long)[URL length]];
    [theRequest setValue:length forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody:[URL dataUsingEncoding:NSUTF8StringEncoding]];

    Connect = [NSURLConnection connectionWithRequest:theRequest delegate:self];

    if (Connect)
    {
        self.receivedData = [NSMutableData data];
    }
    else
    {
        NSError *error;
        [self.delegate connectionFailed:error];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self.delegate connectionSucceed:self.receivedData];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.delegate connectionFailed:error];
}
-(NSString *) urlencode: (NSString *) string
{
    encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)string, NULL, (CFStringRef)@"", kCFStringEncodingUTF16));
    return encodedString;
}


@end

请勿忘记导入您的连接类和委托

@interface yourclass : UIViewController<connectionDelegate>

初始化OfferRequest对象并设置其委托:

-(void)viewWillAppear:(BOOL)animated
{
    connection = [[connectionClass alloc]init];
    connection.delegate = self;
     [connection connectionWithURL:[NSString stringWithFormat:@"pass your url here"]];
}

委托方法将具有您的值

-(void)connectionSucceed:(NSData *)data
{
    NSError *error;
    self.dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    //nslog your data
}

//Connection failure
-(void)connectionFailed:(NSError *)error
{
  //handle error message
}

答案 1 :(得分:0)

好的,我不知道问题究竟在哪里,但是我要做的是修复它,就是创建一个新的init方法,在那里我传递委托:

-(instancetype)initWithDelegate: (id<OfferRequestDelegate>) delegate {
    self = [super init];

    if (self) {
        self.delegate = delegate;
    }

    return self;
}

所以我在VC中初始化OfferRequest对象并传递self,现在它可以正常工作。

答案 2 :(得分:-2)

我猜委托应该是指针对象,而不是对象。

可能对你有所帮助

@property (nonatomic, weak) NSObject <OfferRequestDelegate> *delegate;

并且应该设置你的代表,即确保你的代码被调用。

 [self.delegate addOfferRequest:self dataFromRequest:result];