如何在 UILabel 中显示方法的结果

发布于 2024-11-16 08:35:04 字数 1769 浏览 4 评论 0原文

使用以下代码,我连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

写给空气的情书 2024-11-23 08:35:04

好吧,让我说清楚......您想在同一个 UILabel 中显示多个结果吗?
如果是这样,如果您想一次显示多个结果,您最好使用 UITextView 或更好的 UITableView。 UI 标签的局限性很大。

如果您想向 UILabel 添加更多行,您可以使用

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

“我希望我在理解您的问题上是在正确的轨道上”。

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

textLabel.lineBreakMode = UILineBreakModeWordWrap;
textLabel.numberOfLines = 0;

I hope I was on the right track understanding your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文