2012-08-31 23 views
25

我禁用和使用下面的代碼啓用視圖交互....禁止用戶在視圖IOS

[self.view setUserInteractionEnabled:NO]; 
[self.view setUserInteractionEnabled:YES]; 

如果我不喜歡這樣,它的所有子視圖也得到了影響......所有被禁用,我怎麼只爲特定的觀點?可能嗎?

回答

31

它是完全一樣的,假設你的另一種觀點是要麼成員,或者您可以通過子視圖self.view的遍歷數組,像這樣:

MyViewController.h

UIView* otherView; 

MyViewController.m

otherView.userInteractionEnabled = NO; // or YES, as you desire. 

OR:

for (int i = 0; i < [[self.view subviews] count]; i++) 
{ 
    UIView* view = [[self.view subviews] objectAtIndex: i]; 

    // now either check the tag property of view or however else you know 
    // it's the one you want, and then change the userInteractionEnabled property. 
} 
5
for (UIView* view in self.view.subviews) { 

    if ([view isKindOfClass:[/*"which ever class u want eg UITextField "*/ class]]) 

     [view setUserInteractionEnabled:NO]; 

} 

希望它有幫助。快樂編碼:)

1

最好的選擇是使用Tag屬性的視圖,而不是迭代所有的子視圖。只需將標記設置爲要禁用交互的子視圖,並使用下面的代碼訪問它並禁用交互。

// considering 5000 is tag value set for subView 
// for which we want to disable user interaction 
UIView *subView = [self.view viewWithTag:5000]; 
[subView setUserInteractionEnabled:NO]; 
+0

感謝這對我工作,但我有多個意見,所以我需要隱藏他們所有。我做的另一件事是用背景來看視圖,以便將我禁用的視圖變成灰色。 – Gram

8

在迅速UIView確實有物業userInteractionEnabled,使其響應與否。爲了使全視圖沒有反應的使用代碼:

// make screen unresponsive 
self.view.userInteractionEnabled = false 
//make navigation bar unresponsive 
self.navigationController!.view.userInteractionEnabled = false 

// make screen responsive 
self.view.userInteractionEnabled = true 
//make navigation bar responsive 
self.navigationController!.view.userInteractionEnabled = true