1
我正在使用按鈕操作來更新MySQL表字段的值。更新是在Web服務器中執行的,但我需要在我的視圖控制器中更新UILabel文本。NSURLConnection委託方法
這是我的實現代碼:
- (IBAction)votarAction:(id)sender {
//URL definition where php file is hosted
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
dispatch_async(backgroundQueue, ^{
int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue];
NSString *string = [NSString stringWithFormat:@"%d", categoriaID];
NSLog(@"ID EMPRESA %@",string);
NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="];
[ms appendString:string];
// URL request
NSLog(@"URL = %@",ms);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
//URL connection to the internet
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
dispatch_async(dispatch_get_main_queue(), ^{
//update your label
});
});
}
#pragma NSURLConnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
//buffer is the object Of NSMutableData and it is global,so declare it in .h file
buffer = [NSMutableData data];
NSLog(@"ESTOY EN didReceiveResponse*********");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"ESTOY EN didReceiveDATA*********");
[buffer appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//Here You will get the whole data
NSLog(@"ESTOY EN didFINISHLOADING*********");
NSError *jsonParsingError = nil;
NSArray *array = [NSJSONSerialization JSONObjectWithData:buffer options:0 error:&jsonParsingError];
//And you can used this array
NSLog(@"ARRAY = %@",array);
//HERE LABEL.TEXT UPDATE CODE
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR de PARSING");
NSLog(@"ESTOY EN didFAILWITHERROR*********");
}
正如我告訴你,在MySQL的表中的字段值每按鈕被點擊一次更新,但問題是,NSURLConnection的委託方法從未打過電話 歡迎任何幫助
這是編譯器的好處,它不會使委託回調突然工作,「自我」是由當時最有可能爲零回調是做的。 – Tim
不需要downvote,我的天哪 – klcjr89
「這個答案沒有用」,是downvote的標準。我發佈爲什麼我downvoted所以你可以修改它,我可以調整投票... – Tim