2

I have a Main View Controller that has many subviews. What I want is to disable all other views except one subview and its subviews programmatically from the subview file. But all I get is all frozen views. What did I do wrong?

I tried this code:

#define kDontDisableUserInteraction 321


- (id)initWithFrame:(CGRect)frame
{
    NSLog(@"initWithFrame");
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.tag = kDontDisableUserInteraction;
    }
    return self;
}

-(void)something{

    MVC *myController = [self getMVC];

    for (UIView* subview in myController.view.subviews) {
        NSLog(@"subview.tag %i", subview.tag);
        if (subview.tag != kDontDisableUserInteraction){
            subview.userInteractionEnabled = NO;
        }
    }


    for (UIView *view in self.subviews){
        NSLog(@"enabled!");
        view.userInteractionEnabled = YES;
    }
}

- (MVC *)getMVC {
    Class vcc = [MVC class];    // Called here to avoid calling it iteratively unnecessarily.
    UIResponder *responder = self;
    while ((responder = [responder nextResponder])) if ([responder isKindOfClass: vcc]) return (MVC *)responder;
    return nil;
}
GourmetFan
  • 45
  • 1
  • 6
  • Based on your code above, that 'one subview' is your actual view class – MCKapur Jan 07 '13 at 13:28
  • @Rohan Yes, I want to disable all subviews under the parent view controller, because this object is small and other buttons etc. are still visible, so that is why I want to disable them when this small object appears on the main view. – GourmetFan Jan 07 '13 at 13:35
  • I see, looking at your code, I do not see where it goes wrong... – MCKapur Jan 07 '13 at 13:36
  • Can you put an NSLog(); in the subview.tag != kDontDisableUserInteraction condition and see if it gets called – MCKapur Jan 07 '13 at 13:37
  • @Rohan Exactly, but all objects including objects on this subview are frozen. I cannot see why it happens. – GourmetFan Jan 07 '13 at 13:37
  • @Rohan It gets called four times. And all tags are zero, which is correct. – GourmetFan Jan 07 '13 at 13:40

2 Answers2

2

Following links may be helpful:

How to disable touch input to all views except the top-most view?

UIView -- "user interaction enabled" false on parent but true on child?

Community
  • 1
  • 1
Balakrishna
  • 166
  • 2
  • 13
  • I have virtually tested all codes provided in these links, but all I get is either all frozen views or all objects are clickable. – GourmetFan Jan 07 '13 at 14:46
0

I solved it by applying a full screen of a button on all other views and get the one view that I want to have user interaction upon the button. This way I disallow the user to click on any function except the one view I want the user to click on certain functions.

GourmetFan
  • 45
  • 1
  • 6