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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With