[英]updating UILabel programmatically
我試圖在每次迭代中循環更新UILabel文本但它只顯示最后一個值是否有完成循環所花費的時間約為30-50秒。
這是代碼:
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
[[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];
}
我認為問題是你的線程被你的循環鎖定。 所以你需要在后台執行你的方法並使用它
[self performSelectorInBackground:@selector(myMethod) withObject:nil];
別忘了加入“myMethod”;
-(void)myMethod{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//I do my label update
[pool release];
}
您現在使用它的方式確實取代了文本。 我不知道您要更新哪個標簽。 如果你想要更新lbltest
,那么你應該更改self.lbltest.text = imageName;
to [self.lbltest setText:@"%@ %@", self.lbltest.text, imageName];
這樣,舊文本就不會被新文本替換,但新文本會添加到舊文本中。
使用labelName.text = @"Something";
將該標簽上的文字更改為Something
。
[labelName setText:@"Something"];
。
每當您使用上述兩種中的任何一種時,您都將使用文本“Something”替換該標簽中的文本。 如果要向該標簽添加內容,則必須在新String中包含舊文本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.