【发布时间】:2014-02-21 10:06:00
【问题描述】:
@interface URLClass : NSObject
{
id target;
SEL funObj;
}
+ (URLClass *)sharedInstance;
-(void)theBigFunction:(SEL)func :(id)target;
@property (nonatomic,retain) SEL funObj;
#import "URLClass.h"
static URLClass *instance = NULL;
@implementation URLClass
{
NSMutableData *webData;
}
- (id)init
{
if ( self = [super init] )
{
}
return self;
}
+ (URLClass *)sharedInstance
{
@synchronized([URLClass class])
{
if (!instance)
instance = [[super alloc] init];
return instance;
}
return nil;
}
-(void)theBigFunction:(SEL)func :(id)targetObj{
funObj =func;
target=targetObj;
NSURL *URL = [NSURL URLWithString:@"urlString"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if( connection )
{
webData = [NSMutableData data] ;
}
else
{
NSLog(@"theConnection is NULL");
}
}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return YES;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *error;
id jsonObj = [NSJSONSerialization JSONObjectWithData:webData options:0 error:&error];
if (jsonObj!=nil && error==nil) {
if ([jsonObj isKindOfClass:[NSDictionary class]]) {
NSDictionary *dic=(NSDictionary*)jsonObj;
NSLog(@"DIC jsonObj %@ ",dic);
NSArray *array=[dic objectForKey:@"items"];
NSLog(@"array jsonObj %@ %d",array,[array count]);
}else if ([jsonObj isKindOfClass:[NSArray class]]) {
NSArray *arr=(NSArray*)jsonObj;
NSLog(@"arr jsonObj %@ ",arr);
}
}
[target performSelector:funObj];
// Not gEtting called the aboue line
//performSelector 可能会导致泄漏,因为它的选择器在上面的行中是未知警告 }
当我计划从任何类执行下面的代码时。它不会被调用。
-(void)loginPress:(id)sender{
URLClass *rlOBJ=[URLClass sharedInstance];
[rlOBJ theBigFunction:@selector(upd_Handler:) :self];
}
- (void) upd_Handler: (id) value{
NSLog( @"Seccess");
}
【问题讨论】:
-
你试过用断点调试它吗?在函数的开头放一些NSLog,记录对象的内存,看是否真的被调用/
-
- (void) upd_Handler: (id) value 函数未回调,我已调试,[target performSelector:funObj];没有被执行。
-
调用了BigFunction?
-
是的,它被调用了。我也得到了 JSON 数据。得到 Json 数据后,我想调用 - (void) upd_Handler: (id) value function back
标签: ios objective-c delegates