Touch method not called on UIButton

Discussion in 'iOS Development' started by Blank6, Apr 12, 2010.

  1. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Hey,
    I have a UIView and in it many UIButtons,
    I overided the "touchesBegan" method for the UIView
    but when I touch the UIButton the method isn't called, only outside the buttons.

    Seems like the buttons are blocking the touch, not what I expected.
    I want the method in the UIView to be called when I touch the buttons,
    couldn't find any answers for this in the internet..

    Did I do something wrong or is it expected?
    What can I do about it?

    Thanks ahead
  2. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    TouchesBegan will get called when the user touches the view, as you are touching the UIButton and it is a separate view overlaying the original UIView it will not get called.

    What you want to do is

    [objc][button addTarget:self action

    Please Register or Log in to view images

    selector(aMethod

    Please Register or Log in to view images

    forControlEvents:UIControlEventTouchDown[/objc]

    So whenever the UIButton is touched aMethod will be called.
  3. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Ok,
    and is there any way you can synchronize all touch events in one method?

    Maybe I chose my design wrong and I shouldn't have made a button in the first place,
    I just want a picture that can be touched and moved..
    What do you think?
  4. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    I'm not sure I know what you mean.. care to elaborate?
  5. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    In the method you mentioned you need to specify for each event which method to call,

    isn't there a way to tell the program - I want this button not to block touch events?
    Sort of telling it to be transparent for touches...

    again I'm wondering if maybe I shouldn't have used a button

    ----------------------
    EDIT:

    Never mind, I found on the net a really great solution which is also pretty simple.
    The solution was to inherit UIButton (a thing I already did so it fits great),
    and override the touch method in the following way:

    - (void)touchesBegan: (NSSet *)touches withEvent: (UIEvent *)event
    {
    [[self superview] touchesBegan:touches withEvent:event];
    }

    It's like making the button invisible for touches, but in the touches member you receive exactly which button was pressed

    Please Register or Log in to view images

  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You're overriding the button's UITouch methods. That's a bad idea. When the user touches a button, they expect the button to do something. If you're not following that, then you are likely using the wrong objects.

    If you are using buttons, then, based on the info you've already given us, this is an example of a very poorly designed application.

    Regardless, that one line of code Chicken gave you should have done just fine.
  7. lauNchD

    lauNchD Well-Known Member

    Joined:
    Jan 27, 2008
    Messages:
    1,844
    Likes Received:
    261
    Device:
    iPhone 5 (Black)
    I agree. You can always figure out which button has been pressed in the target's method anyway, so why override UIButton's touch detection?

    If you really need to decide where the touch gets dispatched to, override the -hitTest:withEvent: method (check out its documentation).
  8. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Ok I understand now, but besides that my problem is like Skylar said
    "If you're not following that, then you are likely using the wrong objects."

    What I'm trying to achieve is that when a defined section of the screen is pressed (like a defined rectangle) then an image will appear in that section.
    Using an invisible button that later changes it's image seemed logical, what do you think?

    I will really be happy to hear advices / best practices (I'm a noob ipod programmer^^)
  9. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    So I suggest you should set the UIButton's hidden property to YES

    [objc][button setHidden:YES];[/objc]

    and then adding the target like I showed you last time, in the aMethod change the background image of the UIButton

    [objc][button setBackgroundImage:someImage forState:UIControlStateNormal];[/objc]

    An alternative way although I think it's unnecessary for this is to subclass UIImageView and override the touchesBegan method.
  10. Blank6

    Blank6 New Member

    Joined:
    Sep 8, 2009
    Messages:
    59
    Likes Received:
    0
    Device:
    3G iPod touch
    Ok I tried using the method you mentioned, when the button is touched "aMethod" is called, but I failed to understand how do I receive the NSSet of touches and the UIEvent?

    (When trying to put "touchesBegan::" instead of "aMethod" I receive a "SelectorNotRecognized" exception)


    By the way guys thanks alot for all the help![;

Share This Page