在视图IOS中禁用用户交互

时间:2012-08-31 10:38:29

标签: iphone objective-c ios xcode cocoa-touch

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

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

如果我这样做,所有子视图也会受到影响...所有都被禁用,我该怎么做只针对特定视图?有可能吗?

4 个答案:

答案 0 :(得分:32)

它完全相同,假设您的其他视图是成员,或者您可以遍历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.
}

答案 1 :(得分:12)

在swift DEBUG中,确实有UIView属性以使其响应或不响应。要使完整的View无响应使用代码:

userInteractionEnabled

答案 2 :(得分:5)

for (UIView* view in self.view.subviews) {

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

        [view setUserInteractionEnabled:NO];

}
希望它有所帮助。快乐的编码:)

答案 3 :(得分:1)

最好的选择是使用视图的Tag属性,而不是迭代其所有子视图。只需将标记设置为要禁用交互的subView,并使用下面的代码访问它并禁用交互。

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