How to hide/show the UIimageview?

谁说我不能喝 提交于 2019-12-05 05:56:12
Nik 'Narmo' Dyonin

The problem is you create new UIImageView every time when your view appears. You have to create 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 in -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;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!