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?
在OS X应用程序中,我在滚动视图中都有各种文本字段,按钮和其他控件。有没有办法一次性禁用滚动视图中的元素?
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.
我想避免在每个项目上调用setEnabled:如果我想稍后在滚动视图中添加更多控件,那么维护会更容易。
I'd like to emphasize that this is for an OS X app, so techniques that work in iOS don't necessarily apply here.
我想强调的是,这适用于OS X应用程序,因此在iOS中运行的技术不一定适用于此处。
6 个解决方案
#1
4
You can iterate through all the subviews
of the content view, and call setEnabled:
on all of them. This might break the scroll view by disabling some internal view, so don't iterate on the subview
property of the scroll view itself. To prevent calling setEnabled:
on views that don't have an enabled property, use respondsToSelector:@selector(setEnabled:)
to check.
您可以遍历内容视图的所有子视图,并在所有子视图上调用setEnabled :.这可能会通过禁用某些内部视图来中断滚动视图,因此不要迭代滚动视图本身的子视图属性。要防止在没有enabled属性的视图上调用setEnabled:,请使用respondsToSelector:@selector(setEnabled :)进行检查。
#2
5
You can do this tasks like this easily with bindings. You must have an instance of your controller class in your XIB for the following steps, but the technique does not require this - you can do the setup programmatically if you'd prefer.
您可以使用绑定轻松完成此类任务。您必须在XIB中有一个控制器类的实例用于以下步骤,但该技术不需要这样 - 如果您愿意,可以以编程方式进行设置。
- Add a
BOOL
property to your controller class, sayscrollItemsEnabled
- In IB/designer pane of Xcode 4 open your XIB
- For each controller you'd like enabled/disabled select it and in the bindings pane of the inspector link the
Enabled
binding toscrollItemsEnabled
property of your controller class instance - Now in your code setting the property will enable/disable all the items together
将BOOL属性添加到控制器类,例如scrollItemsEnabled
在Xcode 4的IB /设计器窗格中打开您的XIB
对于您希望启用/禁用的每个控制器,选择它并在检查器的绑定窗格中链接启用绑定到控制器类实例的scrollItemsEnabled属性
现在,在您的代码设置中,属性将启用/禁用所有项目
#3
5
For the sake of the record, here is an NSView category I use in my Cocoa apps:
https://github.com/ardalahmet/DisableSubviews
为了记录,这里是我在Cocoa应用程序中使用的NSView类别: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.
希望能帮助到你。
#4
2
Here is a NSView Category I used in my project which works fine.
这是我在我的项目中使用的NSView类别,它工作正常。
//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
#5
-3
I like to do this in the view controller with one property (a BOOL called userInteractionEnabled or something similar) and then check it in the appropriate delegate methods in the view controller:
我喜欢在视图控制器中使用一个属性(一个名为userInteractionEnabled的BOOL或类似的东西)执行此操作,然后在视图控制器中的相应委托方法中检查它:
- (BOOL)textFieldShouldBeginEditing:(UITextfield *)txtField {
if (!self.userInteractionEnabled) {
return NO;
}
// Your other logic
return YES;
}
Do this for each control type that you use.
对您使用的每种控件类型执行此操作。
#6
-4
Try this
[scrollView setUserInteractionEnabled:NO];
#1
4
You can iterate through all the subviews
of the content view, and call setEnabled:
on all of them. This might break the scroll view by disabling some internal view, so don't iterate on the subview
property of the scroll view itself. To prevent calling setEnabled:
on views that don't have an enabled property, use respondsToSelector:@selector(setEnabled:)
to check.
您可以遍历内容视图的所有子视图,并在所有子视图上调用setEnabled :.这可能会通过禁用某些内部视图来中断滚动视图,因此不要迭代滚动视图本身的子视图属性。要防止在没有enabled属性的视图上调用setEnabled:,请使用respondsToSelector:@selector(setEnabled :)进行检查。
#2
5
You can do this tasks like this easily with bindings. You must have an instance of your controller class in your XIB for the following steps, but the technique does not require this - you can do the setup programmatically if you'd prefer.
您可以使用绑定轻松完成此类任务。您必须在XIB中有一个控制器类的实例用于以下步骤,但该技术不需要这样 - 如果您愿意,可以以编程方式进行设置。
- Add a
BOOL
property to your controller class, sayscrollItemsEnabled
- In IB/designer pane of Xcode 4 open your XIB
- For each controller you'd like enabled/disabled select it and in the bindings pane of the inspector link the
Enabled
binding toscrollItemsEnabled
property of your controller class instance - Now in your code setting the property will enable/disable all the items together
将BOOL属性添加到控制器类,例如scrollItemsEnabled
在Xcode 4的IB /设计器窗格中打开您的XIB
对于您希望启用/禁用的每个控制器,选择它并在检查器的绑定窗格中链接启用绑定到控制器类实例的scrollItemsEnabled属性
现在,在您的代码设置中,属性将启用/禁用所有项目
#3
5
For the sake of the record, here is an NSView category I use in my Cocoa apps:
https://github.com/ardalahmet/DisableSubviews
为了记录,这里是我在Cocoa应用程序中使用的NSView类别: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.
希望能帮助到你。
#4
2
Here is a NSView Category I used in my project which works fine.
这是我在我的项目中使用的NSView类别,它工作正常。
//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
#5
-3
I like to do this in the view controller with one property (a BOOL called userInteractionEnabled or something similar) and then check it in the appropriate delegate methods in the view controller:
我喜欢在视图控制器中使用一个属性(一个名为userInteractionEnabled的BOOL或类似的东西)执行此操作,然后在视图控制器中的相应委托方法中检查它:
- (BOOL)textFieldShouldBeginEditing:(UITextfield *)txtField {
if (!self.userInteractionEnabled) {
return NO;
}
// Your other logic
return YES;
}
Do this for each control type that you use.
对您使用的每种控件类型执行此操作。
#6
-4
Try this
[scrollView setUserInteractionEnabled:NO];