如何在 UILabel 中显示方法的结果
使用以下代码,我连接到 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];
}
欢迎提供示例代码,谢谢。
Using the following code I am connecting to Google API and when I click the button the result of the following method been called will display on label field.
My question is how to display more methods in a label field?
For example I want to display some 4 methods or multiple results in Label field.
In the code below I'm just calling one method and displaying only one result.
I want to display more results or multiple results something similar to Google search.
// .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];
}
Sample code will be welcome, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,让我说清楚......您想在同一个 UILabel 中显示多个结果吗?
如果是这样,如果您想一次显示多个结果,您最好使用 UITextView 或更好的 UITableView。 UI 标签的局限性很大。
如果您想向 UILabel 添加更多行,您可以使用
“我希望我在理解您的问题上是在正确的轨道上”。
Well, let me get this straight... You want to display multiple results within the same UILabel?
If so, if you want to display more than one result at a time, you're best using a UITextView or better yet a UITableView. UI Labels are pretty limiting.
If you want to add more lines to a UILabel though, you can use
I hope I was on the right track understanding your question.