简体   繁体   中英

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"; when UITableView has data, this UILabel will disappear. 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. How can I solve it?

You are trying to remove introductionLabel from its superview , without adding it,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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