[英]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];
}
当UITableView
没有数据时,我要显示“触摸右上角添加”; 当UITableView包含数据时,该UILabel将消失。 添加数据之后,然后重新出现此视图, [introductionLabel removeFromSuperview]
在这里不起作用,但是如果我关闭我的应用程序并重新打开它,则UILabel
将消失。 我该如何解决?
您正在尝试从其superview中删除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];
}
@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
像这样尝试...。每次创建新标签的原因...
@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.