大家好,我是iphone开发的新手。我正在做一些应用,我被卡住了......我有一个文本域,按钮和webview ..
我有一个已解析的json文件..
我想在文本字段中输入一个字符串或方法,它存在于JSON文件中。它应该与json文件中的方法匹配,并且应该在webview上显示方法的内容..
我已经完成了按钮和webview ...我想进一步扩展textfield和webview ..
我们怎么做?请用示例代码建议我......
//代码
- (void)loadData
{
dataWebService = [[NSMutableData data] retain];
NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]retain];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
NSArray* latestLoans = [(NSDictionary*) [responseString JSONValue] objectForKey:@"loans"];
[responseString release];
NSDictionary* loan = [latestLoans objectAtIndex:0];
//fetch the data
NSNumber* fundedAmount = [loan objectForKey:@"funded_amount"];
NSNumber* loanAmount = [loan objectForKey:@"loan_amount"];
float outstandingAmount = [loanAmount floatValue] - [fundedAmount floatValue];
NSString* name = [loan objectForKey:@"name"];
NSString* country = [(NSDictionary*)[loan objectForKey:@"location"] objectForKey:@"country"];
//set the text to the label
label.numberOfLines = 0;
label.text = [NSString stringWithFormat:@"Latest loan: %@ \n \n country: %@ \n \n amount: $%.2f", name,country,outstandingAmount];
}
答案 0 :(得分:1)
我想你问的是:“如何在文本字段中输入部分网址,并将其附加到基本网址并加载到网页视图中?”
如果是这样,你可以很容易地做到。如果您的文本字段名为“textfield”,webview称为“webview”,请执行以下操作:
NSString *baseUrl = @"http://whatever.com";
NSString *fullUrl = [baseUrl stringByAppendingString:textfield.text];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:fullUrl]]];
按下按钮或使用UITextFieldDelegate的- (BOOL)textFieldShouldReturn:(UITextField *)textField
修改强>:
您希望根据您在文本字段中指定的键从NSDictionary中检索JSON生成的值。首先,做
NSString *value = [dictionary objectForKey:textfield.text];
获取值(假设它是一个字符串)。然后,您希望在Webview中显示该值。您可以使用loadHTMLString:baseURL:
或使用javascript通过stringByEvaluatingJavaScriptFromString:
(UIWebView的两种方法)执行此操作。如果你想使用loadHTMLString,你可以放入
NSString *html = [NSString stringWithFormat:@"<html><body>%@</body></html>", value];
[webview loadHTMLString:html baseURL:nil];