Iphone 以编程方式更新UILabel
我试图在每次迭代中更新一个循环中的UILabel文本,但它只显示最后一个值,不管完成循环所花费的时间是30-50秒 代码如下:Iphone 以编程方式更新UILabel,iphone,loops,uilabel,Iphone,Loops,Uilabel,我试图在每次迭代中更新一个循环中的UILabel文本,但它只显示最后一个值,不管完成循环所花费的时间是30-50秒 代码如下: for (float i=0; i< [topicNew count]; i++) { NSDictionary *new= [topicNew objectAtIndex:i]; NSString *imageName = [[[NSString alloc] initWithFormat:@"%@.%@.%@.png", appDelegat
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];
}
for(浮点i=0;i<[topicNew count];i++){
NSDictionary*new=[topicNew objectAtIndex:i];
NSString*imageName=[[NSString alloc]initWithFormat:@“%@.%@.%@.png”,appDelegate.subject,topicNamed,[new objectForKey:kWordTitleKey]]自动删除];
NSString*imagePath=[[self-applicationDocumentsDirectory]stringByAppendingPathComponent:imageName];
NSData*imageData=[self-ParsingImagePath:[new-objectForKey:kWordImagePathKey]];
[progressView setProgress:i/[topicNew count]];
[自我设置进度:i/[topicNew count]];
[self.lbltest setText:@“图像下载…”;
self.lbltest.text=图像名称;
//NSLog(@“发送%f”,i/[topicNew计数]);
//lblpercent.text=[NSString stringWithFormat:@“%d”,i];
[lblpercent setText:[[NSString stringWithFormat:@“%.0f”,i/[topicNew count]]stringByAppendingString:@“%”];
//[self.view addSubview:viewart];
[自我更新标签:self];
NSLog(@“%@,%d”,图像名称,i);
如果(imageData!=nil)
[imageData WriteFile:imagePath原子性:是];
其他的
[[NSFileManager defaultManager]removeItemAtPath:imagePath错误:NULL];
NSMutableDictionary*newWord=[NSMutableDictionary Dictionary With Objects and Keys:[new objectForKey:kWordTitleKey]、kWordTitleKey、[new objectForKey:kWordDefinitionKey]、kWordDefinitionKey、imagePath、kWordImagePathKey、appDelegate.subject、kSubjectKey、topicName、kTopicKey、[new objectForKey:KwordMemberdKey]、KwordMemberdKey、nil];
[newtopicaddobject:newWord];
}
我认为问题在于线程被循环锁定。因此,您需要在后台执行您的方法并使用
[self performSelectorInBackground:@selector(myMethod) withObject:nil];
别忘了写上“我的方法”
您现在使用它的方式确实取代了文本。我不知道你想更新哪个标签。例如,如果您想用一直下载的内容更新
lbltest
,则应更改self.lbltest.text=imageName
至[self.lbltest setText:@“%@%@”,self.lbltest.text,imageName]代码>
这样,旧文本不会被新文本替换,但新文本会添加到旧文本中
使用labelName.text=@“某物”
将该标签上的文本更改为某物
这同样适用于[labelName setText:@“Something”]代码>
无论何时使用上述两种方法中的任何一种,都会将该标签中的文本替换为文本“Something”。如果要在标签中添加内容,必须在新字符串中包含旧文本。回答问题是不可能的。。。毫无疑问。顺便问一下,您在哪里定义了UILabel属性?您是以编程方式还是通过界面生成器生成它?这里必须同意marzapower。在我看来,你有两个问题:你只能得到标签上的最后一个值,而不是所有值。。。你的循环需要很长时间才能完成。请正确定义您的问题,这样有帮助的人就不必问您的问题。没问题,我很高兴我帮助了您!
-(void)myMethod{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//I do my label update
[pool release];
}