JSON响应在警报视图中显示?

时间:2012-11-26 16:29:00

标签: iphone objective-c ios ios5

我是iphone开发的新手,我有一个要求,比如我需要在警报视图中显示JSON响应(例如:当我提供错误的详细信息时,我有一个登录页面从服务器获得响应我需要在警报中显示响应视图) 这里我的代码是

-(IBAction)signin:(id)sender{

            requestSelect = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://abc....?"]];



            NSString *requestString = [NSString stringWithFormat:@"request_parameter={\"EmailId\":\"%@\",   \"Password\":\"%@\"}",email.text,password.text,nil];

            NSLog(@"requestString in subarea %@",requestString);
            NSMutableData *requestData =[NSMutableData dataWithBytes: [requestString UTF8String] length: [requestString length]];

            [requestSelect setHTTPMethod: @"POST"];

            [requestSelect setHTTPBody: requestData];

            connection=[[NSURLConnection alloc] initWithRequest:requestSelect delegate:self];



               }

               - (void)connection:(NSURLConnection *)connection didReceiveResponse:            (NSURLResponse       *)response{
                receiveddata = [[NSMutableData alloc]init ];
             }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)aData{
                 [ receiveddata appendData:aData];
}

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

                  jsonString = [[NSString alloc] initWithData:receiveddata 
                                               encoding:NSUTF8StringEncoding];
            dictServerData = [jsonString JSONValue];

            valueForKey:@"result"]valueForKey:@"data"] objectAtIndex:0]valueForKey:@"service_status" ]);


            arr_login= [[NSArray alloc]initWithArray:[[[[[dictServerData valueForKey:@"webservice"] valueForKey:@"result"]valueForKey:@"data"] objectAtIndex:0]valueForKey:@"service_status" ]];


          objectForKey:@"webservice"]objectForKey:@"result"]objectForKey:@"data"]objectAtIndex:0]objectForKey:@"userdetails" ]objectAtIndex:0]);



              NSMutableDictionary *DetailsDict=[NSMutableDictionary dictionaryWithObject:[   [[[[[dictServerData             objectForKey:@"webservice"]objectForKey:@"result"]objectForKey:@"data"]objectAtIndex:0]objec          tForKey:@"userdetails" ]objectAtIndex:0] forKey:@"data"];

            NSLog(@"details dict : %@",DetailsDict);



            defaults = [NSUserDefaults standardUserDefaults];
            [defaults setObject:DetailsDict forKey:@"details"];

            UpdateDetail *updt =[[UpdateDetail alloc]initWithNibName:@"UpdateDetail" bundle:Nil];
            [self.navigationController pushViewController:updt animated:YES];
            [updt release];



}

1 个答案:

答案 0 :(得分:1)

如果您只想显示带有消息的警报视图,请执行以下操作:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Your Title" 
                                          message: @"Your message"
                                  delegate: nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
[alert show];
[alert release];

这将打开一个模态警报视图(即,直到点击“确定”按钮,才会通过它进行触摸)。

所以只需将JSON响应放入消息中(如果需要,将其转换为可读字符串)