how to display results from a method in UILabel
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&a开发者_JAVA百科mp;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.
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.
精彩评论