簡體   English   中英

ios用viewdidappear更新程序化uilabel的正確方法

[英]ios proper way to update programatic uilabels with viewdidappear

每次加載此選項卡而不是更新動態標簽時,它都會在其上寫一個全新的標簽。 我需要在此代碼中添加什么,以便它對其進行更新或清除動態標簽,然后再添加新標簽。 我覺得它可能只是一個簡單的單行修復。 這是代碼的簡化版本:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM