DavidNg
DavidNg

Reputation: 2836

iOS: setting Exclusive Touch to all buttons in a view

My app has many buttons in a Window and I want to set Exclusive Touch all of them together. Do you have any suggestion about this? Thanks

Upvotes: 1

Views: 10968

Answers (9)

user3408691
user3408691

Reputation: 93

Even though the question was asked many years ago, here is a simple way to set isExclusiveTouch for all subviews that are buttons. This example shows code for subviews of self.view. Change self.view to your view of interest.

Seems like cleaner code for anyone with questions on this in the present.

            for subview in self.view.subviews
        {
            if subview is UIButton {

                subview.isExclusiveTouch = true
            }
        }

Upvotes: 0

Andre Yonadam
Andre Yonadam

Reputation: 1024

Here is some code in swift that will set exclusive touch to all buttons in your viewcontroller's view

for button in self.view.subviews {
    if(button.isKindOfClass(UIButton)){
        (button as! UIButton).exclusiveTouch = true
    }
}

Upvotes: 0

Vineeth
Vineeth

Reputation: 1750

If you want to set exclusiveTouch for ALL UIButtons in your whole application method swizzling will be the perfect solution for you.

This answer explains the way very well : https://stackoverflow.com/a/24534814/976246 , and it works perfectly for me.

Also go through this article to know how this (http://nshipster.com/method-swizzling/) tecknique can be used for various purposes.

Upvotes: 1

day
day

Reputation: 83

-(void)setExclusiveTouchForButtons:(UIView *)myView
{
    for (UIView * v in [myView subviews]) {
        if([v isKindOfClass:[UIButton class]])
            [((UIButton *)v) setExclusiveTouch:YES];
        else if ([v isKindOfClass:[UIView class]]){
            [self setExclusiveTouchForButtons:v];
        }
    }
}

then call this function at viewDidAppear

Upvotes: 2

Augustine P A
Augustine P A

Reputation: 5068

If you are adding buttons pragmatically, then send a message to the button [button setExclusiveTouch:YES]; for each buttons before adding to its super view. Else if you are using xib, you have to send the same message to the button in viewDidLoad or in loadView.

Upvotes: 0

MeloS
MeloS

Reputation: 7938

There is a way to set exclusive touch to all buttons in your app, may be helpful.

#import </usr/include/objc/objc-class.h>

static IMP gOringinalWillMoveToSuperview = nil;

static id newMoveToSuperviewPlusSettingExclusiveTouch(id self,SEL selector,...)
{
    va_list arg_list;
    va_start( arg_list,selector);
    gOringinalWillMoveToSuperview(self,selector,arg_list);
    [self setExclusiveTouch:YES];
    return nil;
}

-(void)addSettingExclusiveTouchToAllUIViewMethodWillMoveToSuperview
{
    gOringinalWillMoveToSuperview = class_getMethodImplementation([UIButton class], @selector(willMoveToSuperview:));
    class_replaceMethod([UIButton class], @selector(willMoveToSuperview:), &newMoveToSuperviewPlusSettingExclusiveTouch, "v@:");
}

if you don't understand this, you can refer to this and this.

Upvotes: 5

DavidNg
DavidNg

Reputation: 2836

I just found an answer for this:

#pragma mark Set Buttons Exclusive Touch Yes
-(void)setExclusiveTouchForButtons:(UIView *)myView
{
    for (UIView * button in [myView subviews]) {
        if([button isKindOfClass:[UIButton class]])
            [((UIButton *)button) setExclusiveTouch:YES];
    }
}

Source

Upvotes: 1

rdelmar
rdelmar

Reputation: 104082

If these buttons are all in the same view, you can loop through the view's subviews, test for whether the particular subview is a button (or test for a tag if you have one set) and set exclusiveTouch on each.

Upvotes: 1

Caleb
Caleb

Reputation: 124997

Are you just looking for an easy way to set them all at once?

If you have all the buttons in an array (e.g. they're all connected to the same IBOutletCollection) you can use key value coding to set the exclusiveTouch property of the array:

[buttonArray setValue:[NSNumber numberWithBool:YES] forKey:@"exclusiveTouch"];

NSArray will then invoke the same method on every item in the array.

Upvotes: 2

Related Questions