button in subview not cancelling touch event passing the to parent view

Discussion in 'iOS Programming' started by gilpster, Apr 5, 2011.

  1. macrumors newbie

    Feb 25, 2011
    Hi all, I've a question about touch events in a view heirarchy:

    I have a UIView object which has an on touch event attached. This has a sub view with another on touch event. When this subview is added to the main view I can override the touch event of the UIView behind it by either adding an event handler for the same event to the subview, or putting an 'point in frame' block in the main view. Both these methods work well.

    However if I add a button to the subview and touch this, then the main view receives the touch event again. How can I stop this?


  2. macrumors regular


    Nov 19, 2010
    Nashville, TN
    So if I'm understanding this correctly you have a View with touch events, a separate View on top with a different touch event, and a button in that View. Could you not just add the button to the parent View? If you are adding the button in IB just drag the button on the main View and use the ruler attributes tab to position it over your subview.

    It should work. How are you adding the subview and button? in IB?

  3. thread starter macrumors newbie

    Feb 25, 2011
    Thanks for replying. I think you understand what i'm trying to do, except that i'm adding the subview in code rather than the IB.

    if I add the button to the main (parent) view then it will be hidden behind the added view, surely?

    Also the button is just an example. The real UIView has an indeterminate amount of controls added at runtime. I'm basically mimicking a menu system of a medical monitor.

  4. macrumors 68000


    Feb 11, 2010
    Subclass your views that you want to be touchable and add the "touch" code to those sub-classed new "custom" UIViews. Take the code out of your primary view controller.

    Basically, you create a new Objective C class and make it a UIView instead of a NSObject class. Then, just add your view via code as you normally would. It will act exactly like a regular view, but all the touch events for it will be sent to your custom class.

Share This Page