简体   繁体   English

iPhone应用程序:代理无响应

[英]iphone app: delegate not responding

So I'm very new to this iphone development stuff.... and I'm stuck. 因此,我对这款iPhone开发工具非常陌生。

I'm building an app that connects to the twitter api. 我正在构建一个连接到Twitter API的应用程序。 However when the connectionDidFinishLoading method gets called, it doesn't seem to recognise the delegate. 但是,当connectionDidFinishLoading方法被调用时,它似乎无法识别委托。

Here's the source code of the request class: 这是请求类的源代码:

#import "OnePageRequest.h"

@implementation OnePageRequest

@synthesize username;
@synthesize password;
@synthesize receivedData;
@synthesize delegate;
@synthesize callback;
@synthesize errorCallBack;
@synthesize contactsArray;

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector{
    //set the delegate and selector
    self.delegate = requestDelegate;
    self.callback = requestSelector;

    //Set up the URL of the request to send to twitter!!
    NSURL *url = [NSURL URLWithString:@"http://twitter.com/statuses/friends_timeline.xml"];

    [self request:url];
}

-(void)request:(NSURL *) url{

    theRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection){
        //Create the MSMutableData that will hold the received data.
        //receivedData is declared as a method instance elsewhere
        receivedData=[[NSMutableData data] retain];
    }else{
        //errorMessage.text = @"Error connecting to twitter!!";
    }
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{

    if ([challenge previousFailureCount] == 0){
        NSLog(@"username: %@ ",[self username]);
        NSLog(@"password: %@ ",[self password]);
        NSURLCredential *newCredential = [NSURLCredential credentialWithUser:[self username]
                                                                    password:[self password]
                                                                 persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
        NSLog(@"Invalid Username or password!");
    }
}   

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    [receivedData setLength:0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [receivedData appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    [theConnection release];
    [receivedData release];
    [theRequest release];

    NSLog(@"Connection failed. Error: %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);

    if(errorCallBack){
        [delegate performSelector:errorCallBack withObject:error];
    }
}

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

    if (delegate && callback){
        if([delegate respondsToSelector:[self callback]]){
            [delegate performSelector:[self callback] withObject:receivedData];
        }else{
            NSLog(@"No response from delegate!");
        }
    }
    [theConnection release];
    [theRequest release];
    [receivedData release];
}

Here's the .h: 这是.h:

@interface OnePageRequest : NSObject {

    NSString *username;
    NSString *password;
    NSMutableData *receivedData;
    NSURLRequest *theRequest;
    NSURLConnection *theConnection;
    id  delegate;
    SEL callback;
    SEL errorCallBack;
    NSMutableArray *contactsArray;
}

@property (nonatomic,retain) NSString *username;
@property (nonatomic,retain) NSString *password;
@property (nonatomic,retain) NSMutableData *receivedData;
@property (nonatomic,retain) id delegate;
@property (nonatomic) SEL   callback;
@property (nonatomic) SEL   errorCallBack;
@property (nonatomic,retain) NSMutableArray *contactsArray;

-(void)friends_timeline:(id)requestDelegate requestSelector:(SEL)requestSelector;
-(void)request:(NSURL *) url;

@end

In the method: connectionDidFinishLoading , the following never gets executed: connectionDidFinishLoading方法中,不会执行以下操作:

[delegate performSelector:[self callback] withObject:receivedData];

Instead I get the message: "No response from delegate" 取而代之的是,我收到消息:“代表未回应”

Anyone see what I'm doing wrong?! 有人看到我在做什么错吗? or what might be causing the problem? 或什么可能导致问题?

 [delegate performSelector:[self callback] withObject:receivedData]; 

(Sorry, my comment about the SEL arguments was due to the difficulty reading the code pasted into the question.) (对不起,我对SEL参数的评论是由于难以读取粘贴到问题中的代码。)

using the debugger in connectionDidFinishLoading: can you verify that the delegate and callback variables are not nil? connectionDidFinishLoading:使用调试器connectionDidFinishLoading:您可以验证delegatecallback变量不是nil吗?


callback is retrieved it has the value: friends_timeline_callback , which is what i'd expect 检索到的回调具有以下值: friends_timeline_callback ,这是我期望的

Next step is to verify that the selector is actually correct. 下一步是验证选择器是否正确。 ie if the callback method has an argument it should be @selector(friends_timeline_callback:) (note the trailing : character to specify that an argument is part of the method signature) 即,如果回调方法具有参数,则应为@selector(friends_timeline_callback:) (请注意结尾的:字符,以指定参数是方法签名的一部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM