I need touch events enabled for a view, but not its superview

Discussion in 'iOS Programming' started by StevenMiles, May 30, 2009.

  1. StevenMiles macrumors newbie

    May 24, 2009

    I have a UIView subview that fills the lower half of the screen, and the UIView contains three UIImageViews.

    I have processing that happens successfully when someone touches or drags anywhere in the UIView subview, but I don't want any touch events to trigger code if the touch happens outside of the UIView, in the upper half of the screen.

    I discovered that if I set user interaction disabled in the main view that contains the UIView subview, then no events happen in the UIView subview either.

    I can't figure out how to enable touch events just in the UIView subview but not on the entire screen. I hope I described my problem well enough.

  2. BlackWolf macrumors regular

    Apr 9, 2009
    Well, you should put your touch-handling code (touchesMoved:withEvent: and those methods) in your "bottom-half-of-screen" viewController class. this way, touches in the upper half get recognized, but since there is no code that handles them, nothing happens.

    or, if for some reason that is not possible, you could simply check in which view the touch occured (UITouch has a property "view")
  3. StevenMiles thread starter macrumors newbie

    May 24, 2009
    Hi, BlackWolf,

    Thank you so much for responding. I didn't realize I could have a view controller for the project, and another view controller for a subview. I'm a newbie, and I can't find an example of anything like that in the books I've read so far. Can you refer me to a reference on how to accomplish that?

  4. BlackWolf macrumors regular

    Apr 9, 2009
    you can have as many view controllers as you want, why shouldn't you?
    don't know about an example, but you just create a view controller like always and do whatever you want with it and add your custom event-handling methods and stuff. the view controller has a property "view" that you can use to add the view as a subview. so, if you are in the superview-controller this would look something like:
    [self.view addSubview:mySecondViewController.view];

    edit: I guess an example, but maybe not a totally easy one, would be
    which has a lot of viewcontrollers in it. actually, almost any application has more than one view controller.

Share This Page