The problem is that you create a new UIImageView every time your view appears. You must create a UIImageView as once:
- (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]; }
and then show / hide it - viewDidAppear method:
- (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; } }
source share