29

我正在使用以下代码禁用和启用视图....

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

如果我这样做,它的所有子视图也会受到影响......所有都被禁用,我该如何只针对特定视图?是否可以?

4

4 回答 4

34

完全相同,假设您的其他视图是成员或者您可以遍历self.view的子视图数组,如下所示:

MyViewController.h

UIView* otherView;

我的视图控制器.m

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

或者:

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.
}
于 2012-08-31T10:45:10.980 回答
13

迅速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
于 2015-12-03T12:12:06.950 回答
5
for (UIView* view in self.view.subviews) {

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

        [view setUserInteractionEnabled:NO];

}

希望能帮助到你。快乐编码:)

于 2012-08-31T10:44:40.770 回答
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];
于 2012-08-31T12:11:28.297 回答