iOS dev
iOS dev

Reputation: 2284

objective c how to set exclusive touch for all UIbuttons on the whole app

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

Answers (5)

huahuahu
huahuahu

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

Sergiy Mumryak
Sergiy Mumryak

Reputation: 61

why so difficult? Make a category

@implementation UIButton (ExclusiveTouch)

- (BOOL)isExclusiveTouch
{
    return YES;
}

@end

Upvotes: 6

CodingMeSwiftly
CodingMeSwiftly

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

Viper
Viper

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

Shai
Shai

Reputation: 25595

Cycle through the subviews (recursively) of the top most view, and for each object of type UIButton, apply exclusive touch

Upvotes: 0

Related Questions