2012-08-31 7 views
25

sto disattivazione e attivazione una vista utilizzando il seguente codice ....Disabilita all'utente di interagire in una vista IOS

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

Se faccio come questo, tutto ciò che subviews anche ottenuto colpito ... Tutti sono disabilitate , come faccio solo per una vista particolare? È possibile?

risposta

31

E 'esattamente lo stesso, assumendo l'altro punto di vista è sia un membro o si può iterare serie di subviews self.view s', in questo modo:

MyViewController.h

UIView* otherView; 

MyViewController.m

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

O:

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]; 

} 

spero che sia d'aiuto. felice di codifica :)

1

L'opzione migliore è quella di utilizzare Tag proprietà della vista, piuttosto che l'iterazione tutte le sue subviews. Basta impostare il tag sulla subView che si desidera disabilitare l'interazione e utilizzare il codice sottostante per accedervi e disabilitare l'interazione.

// 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

Grazie per aver funzionato, tuttavia ho più visualizzazioni, quindi ho dovuto nasconderle tutte. Un'altra cosa che ho fatto è stata creare una vista con uno sfondo in modo da rendere grigia la vista che stavo disattivando. – Gram

8

a Swift UIView hanno proprietà userInteractionEnabled per renderla rispondente o meno. Per rendere il codice di utilizzo completo della risposta Visualizza:

// 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 
Problemi correlati