委托方法不能回调

delegate method cannot callback

委托方法无法回调

这是我的 .h 文件

@protocol ServiceAPIDelegate <NSObject>
@optional
- (void) onRequestLoginFinish:(NSDictionary*) dict;
@end


@interface ServiceAPI : NSObject
+ (id)shareAPI;
@property (nonatomic, weak) id <ServiceAPIDelegate> delegate_service;
;
@end

这是 .m 文件,我使用 ASIFORMData 请求,它在从服务器获得响应后回调到 requestFinished。但是在那之后,我想使用 [self.delegate_service onRequestLoginFinish:result] 将数据发送到 myviewcontroler;在这一行之后。我的程序 运行 通常不是错误,也不是回调函数。我看不到错误在哪里。

 - (void) requestLoginWithUserName:(NSString*) username andPassWord:(NSString*) password {
        NSString* urlString = [PublicInstance API_LOGIN];
        NSArray *keys = PARAMS_ARRAY;
        NSArray *objects = [NSArray arrayWithObjects:username, password, [@((int)En) stringValue], APPID, [PublicInstance getDevideID], DEVIDEOS, nil];
        NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];
        NSString* signData = [PublicInstance signData:dict];
        [dict setObject:signData forKey:signKey];

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:urlString]];
        [request addRequestHeader:@"Content-Type" value:@"application/json"];
        [request setValidatesSecureCertificate:NO];
        [request setRequestMethod:@"POST"];

        NSData *jsonDataToPost = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];

        [request appendPostData:jsonDataToPost];

        [request startAsynchronous];
        [request setDelegate:self];
    }

    - (void)requestFinished:(ASIHTTPRequest *)request
    {
        NSError* error = [request error];
        if(!error) {
            NSString* responseString = [request responseString];
            NSDictionary *result = [NSJSONSerialization JSONObjectWithData;
[self.delegate_service onRequestLoginFinish:result];
}else{
            NSLog(@"%@", [error description]);

        }
    }

ServiceAPI *ShareServiceAPI;

- (id) init {
    if ([self init]) {
        ShareServiceAPI = [ServiceAPI shareAPI];
        ShareServiceAPI.delegate_service =self;
    }
}

- (void) requestLoginWithUserInfor:(UserInfor*) _userinfor {
    [ShareServiceAPI requestLoginWithUserName:_userinfor.username andPassWord:_userinfor.password];
}

##################### 这是委托方法 - 但永远不会回调 (O^-oO)

- (void) onRequestLoginFinish:(NSDictionary *)dict {
    if ([[dict objectForKey:Key_Status] intValue] == 1) {
        NSLog(@"login successful");
    }
    else {
        NSLog(@"login fail....");
    }
}

有人可以帮帮我吗?。谢谢你的时间

您应该始终检查委托是否为 nil,并检查委托是否将选择器响应为:

if(self.delegate_service){
  if([self.delegate_service repondsToSelector:@selector(onRequestLoginFinish:)]){
      [self.delegate_service onRequestLoginFinish:result];
  }
}

通过这种方式可以让你的程序安全,找到方法没有被调用的原因