Reputation: 2284
My app has many buttons through out the application
I want to set Exclusive Touch all of them together at one time. or all views in the app
we can set individually by
[button setExclusiveTouch:YES];
But i want to set at a time for all the buttons in the application
Can we set all view exclusive touch ?
any body have any idea please suggest me.
Upvotes: 4
Views: 7027
Reputation: 101
The most elegant and actually the designed way to do this is by using the appearance
proxy, which is designed to set a defined behaviour or appearance across the board for a given UI component.
[[UIButton appearance] setExclusiveTouch:YES];
For more information: Apple Documentation - UIAppearance and NSHipster - UIAppearance
Upvotes: 10
Reputation: 61
why so difficult? Make a category
@implementation UIButton (ExclusiveTouch)
- (BOOL)isExclusiveTouch
{
return YES;
}
@end
Upvotes: 6
Reputation: 3261
If you really want to set exclusiveTouch
for ALL UIButtons + subclasses in your whole application and not just a single view, you can use method swizzling.
You use the objc runtime to override willMoveToSuperview
and set exclusive touch there.
This is very reliable and i've never had any problems using this technique.
I like doing this especially for UISwitch
, because touch-handling for switches can be a bit tricky and exclusiveTouch
will help avoid bugs caused by simultaneous taps on different switches
Sample taken from the link above and changed so that exclusiveTouch
is set:
#import <objc/runtime.h>
@implementation UIButton (INCButton)
+ (void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class class = [self class];
SEL originalSelector = @selector(willMoveToSuperview:);
SEL swizzledSelector = @selector(inc_willMoveToSuperview:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
BOOL didAddMethod =
class_addMethod(class,
originalSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
swizzledSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
});
}
- (void)inc_willMoveToSuperview:(UIView *)newSuperview
{
// This is correct and does not cause an infinite loop!
// See the link for an explanation
[self inc_willMoveToSuperview:newSuperview];
[self setExclusiveTouch:YES];
}
@end
Create a category and insert this code for each class you want to alter.
Upvotes: 6
Reputation: 1408
You can try this
// Not tested
for (UIView * button in [myView subviews]) {
if([button isKindOfClass:[UIButton class]])
[((UIButton *)button) setExclusiveTouch:YES];
}
Upvotes: 8
Reputation: 25595
Cycle through the subviews (recursively) of the top most view, and for each object of type UIButton, apply exclusive touch
Upvotes: 0