ios用viewdidapear更新程序uilabels的正确方式



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

- (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;
            } 
        }

在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.

最新更新