3

I have 1 method for two buttons in my UI, for touch up inside.

-(void)buttonPressed:(UIButton *)button{
    [yButton setEnabled:NO];
    [iButton setEnabled:NO];
    pismeno = (button.tag == BUTTON_TAG_Y) ? PismenoYpsilon : PismenoJota;
    [self setNewValues];
}

Everything works unless I press BOTH buttons at the EXACT same time. I try to disable them both after touch up inside here:

[yButton setEnabled:NO];
[iButton setEnabled:NO];

enabling them in the end of my setNewValues method. Like said, everything works, unless BOTH buttons are presses at the same time, then it crashes.

How can I eliminate this? Thanks a lot :)

Martin Herman
  • 888
  • 13
  • 34

5 Answers5

15

Martin, you can set the exclusiveTouch property on the UIButton to YES. Then only one button press will be registered.

Look at the exclusive touch property of the UIView class here: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html

Does that help?

clearwater82
  • 1,746
  • 14
  • 9
2

It might help if we know a bit more about the crash?

Without that it is hard to diagnose properly, seeing as CoreUI is Single Threaded you shouldn't be getting two calls into buttonPressed: at the same time so it wont be a race condition.

brain
  • 5,496
  • 1
  • 26
  • 29
0

Sounds like some kind of race condition

Since the button that is pressed is passed as an argument you should probably handle that first, then the other button; maybe also put a @synchronized around them as well for good measure - although that is just a guess.

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • 1
    @synchronised shouldn't help - all touches are on the main thread so they happen sequentially. (I'm assuming a touch won't trigger the creation of a new thread - I'm sure the OP would have mentioned it if it did anything like that!) – deanWombourne Jul 20 '11 at 11:20
0

I don't know why your app is crashing, but I usually use a variable named allowInput or similar.

-(void)buttonPressed:(UIButton *)button{
    if( allowInput == YES ) {
        allowInput == NO;
        [yButton setEnabled:NO];
        [iButton setEnabled:NO];
        pismeno = (button.tag == BUTTON_TAG_Y) ? PismenoYpsilon : PismenoJota;
        [self setNewValues];
    }
}

This has always prevented rapid taps of buttons causing problems for my code.

At the end of setValues, set allowInput to YES.

Maybe this can help you narrow down the cause if using the debugger doesn't find it.

bddckr
  • 1,393
  • 13
  • 11
TigerCoding
  • 8,710
  • 10
  • 47
  • 72
  • Why not just use the `userInteractionEnabled` property - it would stop events even reaching your code which would be more efficient? – deanWombourne Jul 20 '11 at 11:19
  • Actually, userInteractionEnabled is only for each button/object itself, while allowInput is for the whole class, unless userInteractionEnabled can be set for everything in the hierarchy? – TigerCoding Jul 20 '11 at 12:36
  • Yea, you'd have to set it for each ui elemnt you wanted to check/disable. If you have lots of elements then your approach might be better. For a few I'd still stick with userInteractionEnabled though. – deanWombourne Jul 20 '11 at 12:38
0

if your still having issues, want a quick fix? just have to member functions 1 for each button, and then setEnabled on which ever button the function relates too.

theiOSDude
  • 1,480
  • 2
  • 21
  • 36