removeFromSuperview在viewwillappear中不起作用

时间:2014-08-19 04:49:38

标签: ios objective-c uiview

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


}

UITableView没有数据时,我想显示“触摸右上角添加”;当UITableView有数据时,这个UILabel会消失。 在我添加数据后,然后重新显示此视图,[introductionLabel removeFromSuperview]在此处不起作用,但如果我关闭我的应用并重新打开它,UILabel将会消失。 我该如何解决?

3 个答案:

答案 0 :(得分:0)

您正试图从超级视图中删除introductionLabel,而不添加它,

试试这个

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

答案 1 :(得分:0)

@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

尝试这样....每次创建新标签的原因......

答案 2 :(得分:0)

@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