2014-12-30 52 views
0
vwInfo = [[UIView alloc]initWithFrame:CGRectMake(20, 85, 280, 100)]; 
[self.view addSubview:vwInfo]; 

我按鈕tap.when我再次點擊它,我需要檢查vwInfo可見或not.I可以用布爾這樣做編程方式創建一個UIView value.is有任何其他選擇做到這一點?檢查程序創建的UIView可見或不可見

回答

1

您可以使用isDescendantOfView來檢查視圖是否存在但確保您必須通過vwInfo的超級視圖來檢查視圖是否已存在或不存在。

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

thanks..it done the trick – user3823935

+0

@ user3823935如果答案有幫助,那麼upvote或將其標記爲您的問題的答案。 –

+0

我不得不等待兩分鐘才能接受你的答案 – user3823935

0

這是因爲您不想在添加UIView時再次添加UIView嗎? 在這種情況下,你可以在你的.h(頭)申報文件UIView *vwInfo; ,然後在你的IBActionUIButton

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

,您可以檢查與TagFunctionality。將tag分配給您的view

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

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"); 
    } 
} 
0

UIView的是與上海華屬性訪問

如果([vwInfo上海華盈]!=無)

NSLog(@"visible"); 

其他

NSLog(@"not visible");