updating UILabel programmatically
I am trying to updating UILabel text in a loop in every iteration but it is displaying only the last value whether there is time taken for completing the loop is about 30 -50 sec.
Here is the code:
for (float i=0; i< [topicNew count]; i++) {
NSDictionary *new= [topicNew objectAtIndex:i];
NSString *imageName = [[[NSString alloc] initWithFormat:@"%@.%@.%@.png", appDelegate.subject,topicNamed,[new objectForKey:kWordTitleKey]] autorelease];
NSString *imagePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:imageName];
NSData *imageData = [self ParsingImagePath:[new objectForKey:kWordImagePathKey]];
[progressView setProgress:i/[topicNew count]];
[self setProgress:i/[topicNew count]];
[self.lbltest setText:@"Image Downloading..."];
self.lbltest.text =imageName;
//NSLog(@"sending %f",i/[topicNew count]);
//lblpercent.text = [NSString stringWithFormat:@"%d",i];
[lblpercent setText:[[NSString stringWithFormat:@"%.0f",i/[topicNew count]] stringByAppendingString:@"%"]];
//[self.view addSubview:viewalert];
[self updateLabel:self];
NSLog(@"%@,%d",imageName,i);
if(imageData != nil)
[imageData writeToFile:imagePath atomically:YES];
else
开发者_StackOverflow [[NSFileManager defaultManager] removeItemAtPath:imagePath error:NULL];
NSMutableDictionary *newWord = [NSMutableDictionary dictionaryWithObjectsAndKeys:[new objectForKey:kWordTitleKey], kWordTitleKey, [new objectForKey:kWordDefinitionKey], kWordDefinitionKey, imagePath, kWordImagePathKey, appDelegate.subject,kSubjectKey,topicName,kTopicKey,[new objectForKey:kWordMemorizedKey], kWordMemorizedKey, nil];
[newTopic addObject:newWord];
}
I think the problem is your thread is lock by your loop. So you need to perform your method in background and use
[self performSelectorInBackground:@selector(myMethod) withObject:nil];
Don't forget to put in "myMethod";
-(void)myMethod{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//I do my label update
[pool release];
}
The way you are using it now, replaces the text indeed. I don't know which label you'd like to update. If you want to update, for example, lbltest
with what's being downloaded all the time, you should change self.lbltest.text = imageName;
to [self.lbltest setText:@"%@ %@", self.lbltest.text, imageName];
That way, your old text doesn't get replaced by your new text, but your new text gets added to your old text.
Using labelName.text = @"Something";
changes the text on that label to Something
.
The same goes for [labelName setText:@"Something"];
.
Whenever you use any of the two described above, you'll replace the text in that label with the text "Something". If you want to add something to that label, you have to include the old text in your new String.
精彩评论