gpt4 book ai didi

ios - 警报 View 中的响应

转载 作者:行者123 更新时间:2023-11-29 01:00:09 25 4
gpt4 key购买 nike

empty value in alertview我是 IOS 的新手,我想在警报 View 中显示帖子的响应。在 nslog 中我显示了响应。我需要当我单击按钮时警报 View 可以显示我的响应。

编码:

-(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 方法委托(delegate):在这里我在 json111 中得到响应,我在 nslog 中成功显示,但在警报 View 中我失败了

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

关于ios - 警报 View 中的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37110134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com