ios 使用 viewdidappear 更新程序化 uilabels 的正确方法

发布于 2024-12-29 05:24:18 字数 1159 浏览 1 评论 0原文

每次加载此选项卡而不是更新动态标签时,它都会在其上写入一个全新的标签。我需要在这段代码中放入什么,以便它更新它或清除动态标签,然后放入新标签。我觉得这可能只是一个简单的一行修复。这是代码的简化版本:

- (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:YES];
        goalArray = [[NSMutableArray alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://localhost/goal.php"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:test forKey:@"name"];
        [request setDelegate:self];
        [request startAsynchronous];         
    }
    - (void)requestFinished:(ASIFormDataRequest *)request
    {
       ...
            for (id myArrayElement in goalArray) 
            {
                NSLog(@"y value:%i", yValue);
                UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0, yValue, 80, 44)];
                label.font = [UIFont systemFontOfSize:12];
                label.textColor = [UIColor blackColor];
                label.backgroundColor = [UIColor clearColor];
                label.text = myArrayElement;
                [self.view addSubview:label];
                yValue += 44;
            } 
        }

Every time this tab get loaded instead of updating the dynamic label, it writes a brand new label over it. What do I need to put in this code so that it updates it or clears the dynamic labels and then put in the new label. I feel like its probably just a simple one line fix. Here is a simplified version of the code:

- (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:YES];
        goalArray = [[NSMutableArray alloc] init];
        NSURL *url = [NSURL URLWithString:@"http://localhost/goal.php"];
        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
        [request setPostValue:test forKey:@"name"];
        [request setDelegate:self];
        [request startAsynchronous];         
    }
    - (void)requestFinished:(ASIFormDataRequest *)request
    {
       ...
            for (id myArrayElement in goalArray) 
            {
                NSLog(@"y value:%i", yValue);
                UILabel *label =  [[UILabel alloc] initWithFrame: CGRectMake(0, yValue, 80, 44)];
                label.font = [UIFont systemFontOfSize:12];
                label.textColor = [UIColor blackColor];
                label.backgroundColor = [UIColor clearColor];
                label.text = myArrayElement;
                [self.view addSubview:label];
                yValue += 44;
            } 
        }

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

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

发布评论

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

评论(1

微暖i 2025-01-05 05:24:18

在 requestFinished: 的 for 循环之前,添加以下内容:

for ( UIView *die in [self subviews]) {   // clear out previous label
    if ( die.tag == 123 ) {
       [die removeFromSuperview];
    }
}

然后在 for 循环内添加以下内容:

label.tag = 123;  //doesn't have to be 123, as long as it's an int that matches the one above.

In requestFinished: before the for loop, add this:

for ( UIView *die in [self subviews]) {   // clear out previous label
    if ( die.tag == 123 ) {
       [die removeFromSuperview];
    }
}

and then inside your for loop, add this:

label.tag = 123;  //doesn't have to be 123, as long as it's an int that matches the one above.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文