2014-03-01 198 views
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的委託方法從未打過電話 歡迎任何幫助

回答

1

在您的視圖控制器的頭文件中加入:<NSURLConnectionDelegate>

而且,沒有必要扔NSURLConnection的到一個單獨的後臺進程,也許這就是爲什麼代表們不叫。 NSURLConnection的已經是異步

也許嘗試這樣的事:

- (IBAction)votarAction:(id)sender 
{ 
    int categoriaID = [[detalleDescription objectForKey:@"idEmpresa"] intValue]; 

    NSString *originalString = [NSString stringWithFormat:@"%d", categoriaID]; 

    NSMutableString *mutablesString = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/cambiarvaloracionempresa.php?id="]; 
    [mutableString appendString:originalString]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:mutableString]]; 
    request.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData; 
    request.timeoutInterval = 5.0; 

    [NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler: 
    ^(NSURLResponse *response, NSData *data, NSError *connectionError) 
    { 
     if (data) 
     { 
      NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 

      dispatch_async(dispatch_get_main_queue(),^
      { 
       // Update your label 
       self.label.text = [array objectAtIndex:someIndex]; 
      }); 
     } 
     else 
     { 
      // Tell user there's no internet or data failed 
     } 
    }]; 
} 
+0

這是編譯器的好處,它不會使委託回調突然工作,「自我」是由當時最有可能爲零回調是做的。 – Tim

+0

不需要downvote,我的天哪 – klcjr89

+0

「這個答案沒有用」,是downvote的標準。我發佈爲什麼我downvoted所以你可以修改它,我可以調整投票... – Tim

相關問題