当我按下按钮时,它会调用function1
。只有当我多次按下按钮时,function1
sendAsynchronousRequest
才有效。否则,在它第一次调用时,它内部的代码不会被调用。
我怎样才能让它发挥作用?它的原因是什么?谢谢。
-(BOOL)function1{
....
theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/IsAuthenticatedNA" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data){
result=TRUE;
}
else if (error)
NSLog(@"%@",error);
result=FALSE;
}];
return result;
}
-(IBAction)login:(id)sender{
BOOL m=[self function1];
if(m){
//do something
}else{
//do something
}
}
答案 0 :(得分:1)
试试这个。
-(void)function1{
....
theRequest = [[NSMutableURLRequest alloc]initWithURL:locationOfWebService];
NSString *msgLength = [NSString stringWithFormat:@"%d",[soapFormat length]];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://tempuri.org/IsAuthenticatedNA" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
//the below encoding is used to send data over the net
[theRequest setHTTPBody:[soapFormat dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
if (data){
//get the value of result as BOOL
if(/* BOOL is YES */){
//do something
}else{
//do something
}
}
else if (error)
NSLog(@"%@",error);
}];
}
-(IBAction)login:(id)sender{
[self function1];
}