警报视图中的响应

Response in alert view

我是 IOS 的新手,我想在警报 view.in nslog 中显示来自 post 的响应,我显示了响应。我需要当我单击按钮时警报视图可以显示我的响应。

编码:

-(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];
    }
}

查看:

- (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];

}

post 方法委托: 在这里,我在 json111 中得到响应,我在 nslog 中成功显示,但在警报视图中我失败了

-(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]

把这个改成

更新答案

@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
}
}

你应该了解NSString的基础知识,参考here

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

您可以将响应保存在 NSString 中,然后将此字符串传递到警报视图的消息字段中。

但是,从 iOS 8 开始,

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead

如下更改您的警报视图:

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

这将显示您的回复消息,而不是 %@..

如果你想在点击alertView时向服务器发送数据,你需要在alertView Delegate中编写代码clickedButtonAtIndex

希望对您有所帮助..