清空多个UILabel值

时间:2016-10-18 11:14:30

标签: ios objective-c iphone ios7 uilabel

我有多个uilabels值。 当我点击按钮NEXT时,我需要清空UILabels self.lbl_title。

我该怎么做?

self.lbl_title.hidden=true在这种情况下不起作用。

-(void)fetchdata
{

    int y=10;
    if(arrayquestion.count!=0){
    for (int i=0;i<arrayquestion.count;i++)
    {
        CGSize textsize = [[[arrayquestion objectAtIndex:i] valueForKey:@"question_title"] sizeWithFont:[UIFont systemFontOfSize:18] constrainedToSize:CGSizeMake(850, MAXFLOAT) lineBreakMode:NSLineBreakByCharWrapping];

        self.lbl_title=[[UILabel alloc] init];
        self.lbl_title.frame= CGRectMake(60,y-3,900,textsize.height+5);
        self.lbl_title.text=[[arrayquestion objectAtIndex:i] valueForKey:@"question_title"];
        self.lbl_title.backgroundColor=[UIColor clearColor];
        self.lbl_title.numberOfLines=0;
        self.lbl_title.font=[UIFont systemFontOfSize:18];
         y=y+textsize.height+30;

        [self.scrll_vw addSubview:self.lbl_title];

    }

    }
   else{
       NSLog(@"%s","Yes");
       self.lbl_title.hidden = YES;

    }  
}

-(IBAction)Next:(id)sender
{
    [arrayquestion removeAllObjects];
      [self fetchdata];
}

3 个答案:

答案 0 :(得分:1)

如果您只想清除标签文字,可以使用

self.lbl_title.text = @"";

答案 1 :(得分:1)

您还可以尝试重新加载scrollview,因为您需要清除UILable的旧参考

删除旧UILable,然后调用您的方法:[self fetchdata]         它会挑衅地工作

-(IBAction)next:(id)sender
{
 for (UILabel *view in self.contentView.subviews)
    {
        if (![UILabel isKindOfClass:[UIImageView class]])
            [UILabel removeFromSuperview];
    }
    [arrayquestion removeAllObjects];
      [self fetchdata];
}

答案 2 :(得分:0)

您可以遍历scrollView中的所有子视图,找到标签并将其清空或隐藏。类似的东西:

[self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

     //if you have only one label use the following:
     if ([obj isKindOfClass:[UILabel class]]){
         [obj setHidden:YES]
     }
    }];

或者,如果您有多个UILabel,您可以为特定标签指定一个类似100的标签值并使用:

[self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

         //if you have only one label use the following:
         if (obj.tag == 100){
             [obj setHidden:YES]
         }
   }];