警报视图中的响应

时间:2016-05-09 07:38:21

标签: ios objective-c nsurlconnection uialertview

empty value in alertview我是IOS的新手我希望在警报视图中显示帖子的响应。在nslog中我显示了响应。当我点击按钮警报视图时我需要显示我的回复。

编码:

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

}

发布方法代表: 在这里我得到了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]

4 个答案:

答案 0 :(得分: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
}
}

答案 1 :(得分:0)

您应该了解NSString的基础知识,请参阅here

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

答案 2 :(得分:0)

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

但是,从iOS 8开始,

  

UIAlertView已弃用。使用UIAlertController和UIAlertControllerStyleAlert的preferredStyle代替

答案 3 :(得分:0)

更改您的警报视图如下:

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

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

如果要在单击alertView上将数据发送到服务器,则需要在alertView Delegate clickedButtonAtIndex

中编写代码

希望有所帮助......