Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa: best way to disable all controls in a view

In an OS X app, I have various text fields, buttons, and other controls all inside of a scroll view. Is there a way to disable the elements inside the scroll view all at once?

I'd like to avoid calling setEnabled: on each and every item, so that maintenance is easier if I want to add more controls to the scroll view later on.

I'd like to emphasize that this is for an OS X app, so techniques that work in iOS don't necessarily apply here.

like image 309
pepsi Avatar asked Mar 08 '12 14:03

pepsi


2 Answers

For the sake of the record, here is an NSView category I use in my Cocoa apps:
https://github.com/ardalahmet/DisableSubviews

It makes it easy to enable/disable subviews at once and it also offers much flexibility.
You can make such calls:

[scrollView disableSubviews:YES];

[self.window.contentView disableSubviews:YES
                                  ofType:[NSTextField class]];

[someView disableSubviews:YES
                   filter:^BOOL (NSView *v) {
                       return [v isKindOfClass:[NSTextField class]] &&
                              (((NSTextField *) v).stringValue.length < 1);
                   }];

[otherView disableSubviews:disable
                  startTag:3
                    endTag:7];

Hope it helps.

like image 71
Ahmet Ardal Avatar answered Sep 22 '22 18:09

Ahmet Ardal


Here is a NSView Category I used in my project which works fine.

//Code for NSView+Custom.h
#import <Cocoa/Cocoa.h>
@interface NSView (Custom)
    -(void) setEnabled:(BOOL) isEnabled;
@end

//Code for NSView+Custom.m

#import "NSView+Custom.h"

@implementation NSView (Custom)

-(void) setEnabled:(BOOL) isEnabled{

    for (NSView* subView in self.subviews) {

        if ([subView isKindOfClass:[NSControl class]]) {

            [(NSControl*)subView setEnabled:isEnabled];
        }else  if ([subView isKindOfClass:[NSView class]]) {

            [subView setEnabled:isEnabled];
        }
    }
}

@end
like image 32
tojohere Avatar answered Sep 23 '22 18:09

tojohere