- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
CGRect viewRect = CGRectMake(250, 100, 30, 30);
as = [[UIImageView alloc] initWithFrame:viewRect];
as.backgroundColor=[UIColor clearColor];
UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"check" ofType:@"png"]];
[as setImage:img];
[self.view addSubview:as];
BOOL test= [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
NSLog(@"%@", (test ? @"YES" : @"NO"));
if(test == YES)
{
as.hidden=NO;
}
else
{
as.hidden=YES;
}
}
test
结果YES
但imageView
不会监听命令.hidden
或每次viewDidAppear
时更新。如果不是{我重新启动应用程序,然后在我将其转为“是”之后它就消失了,但是我完全表现出来,但是我永远不会总是在那里,我无法隐藏它。
知道为什么没有反应吗?
答案 0 :(得分:12)
问题是,每当您的视图出现时,您都会创建新的UIImageView
。您必须一次创建UIImageView
:
- (void)loadView {
[super loadView];
CGRect viewRect = CGRectMake(250, 100, 30, 30);
as = [[UIImageView alloc] initWithFrame:viewRect];
as.backgroundColor = [UIColor clearColor];
UIImage *img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"check" ofType:@"png"]];
as.image = img;
[self.view addSubview:as];
[as release];
}
然后在 - viewDidAppear
方法中显示/隐藏它:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"switch"];
NSLog(@"%@", (test ? @"YES" : @"NO"));
if(test == YES) {
as.hidden = NO;
}
else {
as.hidden = YES;
}
}