【问题标题】:how to display results from a method in UILabel如何在 UILabel 中显示方法的结果
【发布时间】: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


    【解决方案1】:

    好吧,让我直截了当地说...您想在同一个 UILabel 中显示多个结果吗? 如果是这样,如果你想一次显示多个结果,你最好使用 UITextView 或更好的 UITableView。 UI 标签非常有限。

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

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

    我希望我理解你的问题是正确的。

    【讨论】:

    • 是的,你的仪式我想在同一个 UILabel 字段中显示更多结果...结果应该逐行显示...但我没有得到代码来做 tat...wat 更改我在上面的代码中 shd make?
    猜你喜欢
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多