简体   繁体   中英

Check programmatically created UIView visible or not

vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
[self.view addSubview:vwInfo];

I create a UIView programmatically on button tap.when i click it again i need to check vwInfo is visible or not.I can done this with a Boolean value.is there any other option to do this?

you can check if view is exist or not using isDescendantOfView but make sure you have to pass your vwInfo 's superview to check view already exist or not.

if ([vwInfo isDescendantOfView:self.view]) {
    //view already exist in self.view
}
else{
   //view is not exist in self.view.
}

Is it because you do not want to add the UIView a second time when you already added it ? In this case you could declare in your .h(header) file UIView *vwInfo; and then in your IBAction for your UIButton :

if(vwInfo == nil) {
   vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
   [self.view addSubview:vwInfo];
}

you can check with Tag Functionality . Assign tag to your view .

UIView * vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)];
[vwInfo setTag:101];
[self.view addSubview:vwInfo];

On Button Click

- (void)buttonClick :(id)sender {

    UIView * viewTemp = (UIView *)[self.view viewWithTag:101];

    if(viewTemp){
        NSLog(@"View is available");
        if([viewTemp isHidden]){
            NSLog(@"Your view is hidden");
        }else{
            NSLog(@"Your view is visible");
        }
    }else{
        NSLog(@"View is not added yet");
    }
}

UIView is accessible with the superview property

if([vwInfo superview]!=nil)

NSLog(@"visible");

else

NSLog(@"not visible");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM