以编程方式更新 UILabel

发布于 2024-11-05 18:30:11 字数 1607 浏览 3 评论 0原文

我试图在每次迭代中循环更新 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];

}

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
        [[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];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰火雁神 2024-11-12 18:30:11

我认为问题是你的线程被循环锁定。所以你需要在后台执行你的方法并使用

[self performSelectorInBackground:@selector(myMethod) withObject:nil];

不要忘记放入“myMethod”;

-(void)myMethod{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//I do my label update
[pool release];
}

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];
}
柠檬 2024-11-12 18:30:11

您现在使用它的方式确实取代了文本。我不知道您要更新哪个标签。例如,如果您想更新 lbltest 以及一直在下载的内容,则应将 self.lbltest.text = imageName; 更改为 [self. lbltest setText:@"%@ %@", self.lbltest.text, imageName];

这样,您的旧文本不会被新文本替换,但新文本会添加到旧文本中。

使用 labelName.text = @"Something"; 将该标签上的文本更改为 Something

[labelName setText:@"Something"]; 也是如此。

每当您使用上述两者中的任何一个时,您都会将该标签中的文本替换为文本“Something”。如果您想向该标签添加某些内容,则必须将旧文本包含在新字符串中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文