2016-05-09 6 views
0

empty value in alertview Je suis nouveau à IOS je veux afficher la réponse de la poste dans la vue d'alerte. Dans nslog j'ai montré la réponse. J'ai besoin quand j'ai cliqué sur l'affichage d'alerte de bouton peut afficher ma réponse.Response in alert view

codage:

-(void) sendDataToServer : (NSString *) method params:(NSString *)str{ 


    NSData *postData = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[str length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:URL]]; 
    NSLog(@"%@",str); 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 

    if(theConnection){ 

     mutableData = [[NSMutableData alloc]init]; 
    } 
} 

alerview:

- (IBAction)butt1:(id)sender { 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value" 
                message:@"%@" 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"ok", nil]; 

    [self sendDataToServer :@"POST" params:str]; 

    [alert show]; 

} 

délégués après la méthode: ici i obtenir une réponse à json111 que je montrais à NSLog mais avec succès en vue d'alerte i FAILED

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [mutableData appendData:data]; 
} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    return; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 


    NSError* error111; 
    json111 = [NSJSONSerialization JSONObjectWithData: mutableData 
              options:kNilOptions 
              error:&error111]; 
    NSLog(@"%@",json111); 



} 

[![emptyvalue in alertview][1]][1] 

Répondre

1

changer cela en

réponse mis à jour

@interface myViewController : UIViewController <UIAlertViewDelegate> 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 


NSError* error111; 
json111 = [NSJSONSerialization JSONObjectWithData: mutableData 
             options:kNilOptions 
             error:&error111]; 
NSLog(@"%@",json111); 

NSArray *temp = [ json111 objectForKey:@"Currencies"]; 

// you can directly fetch like 
NSDictionary *fetchDict = [temp objectAtIndex:0]; 
    NSDictionary *fetchUnit = [temp objectAtIndex:1]; 
// finally you can fetch like 
    NSString * total = [fetchDict objectForKey:@"total"]; 
    NSString * one_unit = [fetchUnit objectForKey:@"one_unit"]; 

    //updated 
    NSString *final = [NSString stringWithFormat:@"%@ , %@", total,one_unit]; 

// in here you can assign the value to Alert 


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value" 
               message:final 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"ok", nil]; 

alert.tag = 100; 
[alert show]; 


} 




- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (alertView.tag == 100) 
{ 
if (buttonIndex == 0) 
{ 
    // ok button pressed 
    //[self sendDataToServer :@"POST" params:str]; 
}else 
{ 
    // cancel button pressed 
} 
} 
+1

UIAlertView est déconseillés dans iOS9, à cet endroit l'utilisation UIAlertviewController –

+0

i besoin valeur de réponse de méthode post pas de valeur textfield @ Anbu.Karthik –

+0

@ A.sonu oh glace, attendez que je modifie mon Anser –

0

Vous devez connaître les bases de NSString, reportez-vous here.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value" 
                message:[NSString stringWithFormat:@"%@", str] 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"ok", nil]; 
0

Vous pouvez enregistrer votre réponse dans un NSString, puis transmettre cette chaîne dans le champ de message de votre vue d'alerte.

Cependant, comme d'iOS 8,

UIAlertView est dépréciée. Utilisez UIAlertController avec un preferredStyle de UIAlertControllerStyleAlert au lieu

0

Modifier votre alerte Voir la manière suivante:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Value" 
               message:[NSString stringWithFormat:@"Response Message : %@",str] 
               delegate:self 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"ok", nil]; 

Cela permet d'afficher votre message de réponse à la place% de @ ..

Si vous souhaitez envoyer des données à Server on Cliquez sur alertView, vous devez entrer le code dans alertView Déléguer clickedButtonAtIndex

Espérons-le .. lps