【发布时间】:2011-06-20 06:57:49
【问题描述】:
我使用以下代码连接到 Google API,当我单击按钮时,调用以下方法的结果将显示在标签字段上。
我的问题是如何在标签字段中显示更多方法?
例如我想在标签字段中显示一些 4 种方法或多个结果。
在下面的代码中,我只是调用一种方法并只显示一个结果。
我想显示更多结果或类似于 Google 搜索的多个结果。
// .h file
{
IBOutlet UILabel* label;
NSMutableData *dataWebService;
}
@property (retain, nonatomic) NSMutableData *dataWebService;
-(IBAction)loadData;
// .m file
- (void)loadData
{
dataWebService = [[NSMutableData data] retain];
NSURLRequest *request = [[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures"]]retain];
[[NSURLConnection alloc]initWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[dataWebService setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[dataWebService appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Error during connection: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding];
self.dataWebService = nil;
// NSDictionary *dictionary = [responseString JSONValue];
NSDictionary *dictionaryReturn = (NSDictionary*) [[responseString JSONValue] objectForKey:@"context"];
[responseString release];
NSString *name = (NSString*) [dictionaryReturn objectForKey:@"title"];
label.text = [NSString stringWithFormat:@"lectures title: %@",name];
}
欢迎提供示例代码,谢谢。
【问题讨论】:
标签: iphone objective-c xcode uilabel