iphone应用程序:代理未响应
所以我对iphone的开发非常陌生。。。。我被卡住了 我正在构建一个连接到twitter api的应用程序。但是,当调用ConnectiondFinishLoading方法时,它似乎无法识别委托 以下是请求类的源代码:iphone应用程序:代理未响应,iphone,delegates,Iphone,Delegates,所以我对iphone的开发非常陌生。。。。我被卡住了 我正在构建一个连接到twitter api的应用程序。但是,当调用ConnectiondFinishLoading方法时,它似乎无法识别委托 以下是请求类的源代码: #import "OnePageRequest.h" @implementation OnePageRequest @synthesize username; @synthesize password; @synthesize receivedData; @synthesiz
#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];
}
以下是.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
在方法:connectiondFinishLoading
中,永远不会执行以下操作:
[delegate performSelector:[self callback] withObject:receivedData];
相反,我得到的信息是:“代表没有回应”
谁知道我做错了什么?!或者是什么导致了这个问题
(很抱歉,我对SEL
参数的评论是因为难以阅读粘贴到问题中的代码。)
使用connectiondFinishLoading:
中的调试器,可以验证委托
和回调
变量是否为零吗
检索回调时,它的值为:
friends\u timeline\u callback
,这是我所期望的
下一步是验证选择器是否正确。也就是说,如果回调方法有一个参数,它应该是“代码> @选择器(FrutsSimeleNeL.CalBe后撤:))/<代码>(请注意尾随<代码>:< /Cord>字符以指定一个参数是方法签名的一部分)< /P>你好,菲奥娜,请编辑您的问题以正确地格式化代码。只需选择代码部分,并使用“代码”按钮使其自动缩进。。调试时,执行以下语句:如果([delegate respondsToSelector:[self callback]]):回调超出范围。。。但是,当执行此语句并检索callback的值时,它的值为:friends\u timeline\u callback,这是我所期望的。
[delegate performSelector:[self callback] withObject:receivedData];