简体   繁体   English

removeFromSuperview在视图中将不起作用

[英]removeFromSuperview does't work in the viewwillappear

-(void)viewWillAppear:(BOOL)animated
{
       [super viewWillAppear:animated];

       NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
       NSData *dateListData=[defaults objectForKey:@"DateListData"];
       self.listOfDate=[NSKeyedUnarchiver unarchiveObjectWithData:dateListData];

       UILabel *introductionLabel=[[UILabel alloc]initWithFrame:CGRectMake(57, self.view.frame.size.height/2, 290, 50)];
       introductionLabel.textColor=[UIColor grayColor];
       introductionLabel.text=@"touch the right corner to add";
       introductionLabel.font=[UIFont systemFontOfSize:18];

       if ([self.listOfDate count]==0)
       {
              [self.view addSubview:introductionLabel];
       }
       else
       {
              [introductionLabel removeFromSuperview];
       }

       [self.tableView reloadData];


}

When the UITableView has no data, I want to show the "touch the right corner to add"; UITableView没有数据时,我要显示“触摸右上角添加”; when UITableView has data, this UILabel will disappear. 当UITableView包含数据时,该UILabel将消失。 After I add the data, then re-appear this view, [introductionLabel removeFromSuperview] doesn't work here, but if I close my app, and reopen it, the UILabel will disappear. 添加数据之后,然后重新出现此视图, [introductionLabel removeFromSuperview]在这里不起作用,但是如果我关闭我的应用程序并重新打开它,则UILabel将消失。 How can I solve it? 我该如何解决?

You are trying to remove introductionLabel from its superview , without adding it, 您正在尝试从其superview中删除introductionLabel ,而不添加它,

Try this 尝试这个

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *dateListData=[defaults objectForKey:@"DateListData"];
    self.listOfDate=[NSKeyedUnarchiver unarchiveObjectWithData:dateListData];

    UILabel *introductionLabel=[[UILabel alloc]initWithFrame:CGRectMake(57,     self.view.frame.size.height/2, 290, 50)];
    introductionLabel.textColor=[UIColor grayColor];
    introductionLabel.text=@"touch the right corner to add";
    introductionLabel.font=[UIFont systemFontOfSize:18];
    [self.view addSubview:introductionLabel];
    [introductionLabel setHidden:self.listOfDate.count]
    [self.tableView reloadData];
}
@interface yourClassName ()
{
    UILabel *_introductionLabel;
}
@end

@implementaion yourClassName

-(void)viewDidLoad
{
....
_introduction =[[UILable alloc]init];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *dateListData=[defaults objectForKey:@"DateListData"];
    self.listOfDate=[NSKeyedUnarchiver unarchiveObjectWithData:dateListData];

    _introductionLabel.frame=CGRectMake(57, self.view.frame.size.height/2, 290, 50)];
    _introductionLabel.textColor=[UIColor grayColor];
    _introductionLabel.text=@"touch the right corner to add";
    _introductionLabel.font=[UIFont systemFontOfSize:18];

    if ([self.listOfDate count]==0)
    {
        [self.view addSubview:_introductionLabel];
    }
    else
    {
        [_introductionLabel removeFromSuperview];
    }

    [self.tableView reloadData];


}

@end

Try like this.... the reason each and every time new label is created... 像这样尝试...。每次创建新标签的原因...

@interface className ()
{
    UILabel *yourLabel;
}
@end

@implementaion className

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *dateListData=[defaults objectForKey:@"DateListData"];
    self.listOfDate=[NSKeyedUnarchiver unarchiveObjectWithData:dateListData];

if(!yourLabel){
   yourLabel=[[UILabel alloc]initWithFrame:CGRectMake(57, self.view.frame.size.height/2, 290, 50)];
    yourLabel.textColor=[UIColor grayColor];
    yourLabel.text=@"touch the right corner to add";
    yourLabel.font=[UIFont systemFontOfSize:18];
}

    if ([self.listOfDate count]==0)
    {
        [self.view addSubview:yourLabel];
    }
    else
    {
        [yourLabel removeFromSuperview];
    }

    [self.tableView reloadData];    
}

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM