2

I have the main View controller which adds 2 subviews.

- (void)viewDidLoad
{
    [self init];
    [super viewDidLoad];

    //To shrink the view to fit below the status bar.
    self.wantsFullScreenLayout = YES;

    //We start off by displaying the background Images.
    [self displayMenuSceneBackground];

    //Then we show the Menus.
    [self displayMenuSceneMenu];


}

Here we add the subviews to the main View Controller. both subviews are view controllers built using interface builder.

-(void) displayMenuSceneBackground{
    //Display the MenuSceneBackground View Controller
    MenuSceneBackground *screen = [[MenuSceneBackground alloc] init];

    [self.view addSubview:screen.view];
    [screen release];
}

-(void) displayMenuSceneMenu{
    //Display the MenuSceneMenu View Controller
    MenuSceneMenu *screen = [[MenuSceneMenu alloc] init];

    [self.view addSubview:screen.view];
    [screen release];
}

Both subviews display correctly, even some animations in the MenuSceneBackground view controller work fine, but none of these subviews receive touch events.

They all implement touchesBegan but only the main View Controller receives them.

I tried making the main View Controller userInteraction to NO, and not implementing the touchesBegan method but this only makes the touches to be ignored.

Both subviews display on the whole screen size.

I did read similar problems but the responses were of no help.

I have this in the sub views

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

    NSLog(@"testing MenuSceneMenu");
    [self clicked_testing_alert];    //This is a UIAlertView for debugging
    return;
}

Thanks for the help in advance.

Camilo Olea
  • 23
  • 1
  • 5

2 Answers2

2

Please note that an UIViewController isn't the same as an UIView and thus isn't receiving touchesBegan-events. (you added touchesBegan in your view controller instead of in your view...).

Solution: thanks to Simon Goldeen, I found out that in a demo-project, touchesBegan gets called. However, in my production-projects, it doesn't. Here's the thing: (as Apple recommends) you and I are using - (void)release; to decrease memory usage. This causes touchesBegan not to be called. Simon doesn't use release, so in his situation, they do get called.

titaniumdecoy
  • 18,900
  • 17
  • 96
  • 133
cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • 3
    UIViewController is a subclass of UIResponder and receives the -`touchesBegan:` method call if its view doesn't respond to the method. – Simon Goldeen May 03 '11 at 17:57
  • It doesn't. Which makes sense, because you cannot touch the view controller, you touch the view owned (controlled) by the view controller... – cutsoy May 03 '11 at 18:01
  • I understand you responses. I will go now and do some changes to the code. Thanks for the quick responses both of you. brb – Camilo Olea May 03 '11 at 18:08
  • 1
    I just wrote a quick test app to verify and UIViewController *does* recieve the `-touchesBegan:` method call. – Simon Goldeen May 03 '11 at 18:10
  • No way :|... #is writing an test app himself... **Edit**: I'm sorry, it indeed does (which just doesn't make sense, since some parts of the docs even say it doesn't work)... – cutsoy May 03 '11 at 18:12
  • True. Because my main View Controller in this question is indeed receiving the touches, the ones not receiving it are the subviews view controllers. – Camilo Olea May 03 '11 at 18:18
  • @CamiloOlea I just edited my answer (adding "Solution: ..."), try that :) – cutsoy May 03 '11 at 18:20
  • Tim, I would like to know which documentation you found that said that would not work, as I found two places that say it does. [Link 1](http://developer.apple.com/library/ios/#documentation/general/conceptual/Devpedia-CocoaApp/Responder.html) [Link 2](http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/EventsiPhoneOS/EventsiPhoneOS.html) – Simon Goldeen May 03 '11 at 18:21
  • Does it matter if I set the `UserInteractionEnabled = YES;` in interface builder rather than programatically? – Camilo Olea May 03 '11 at 18:22
  • @Camilo Nope (unless you unset it programmatically, of course). Did you try removing the release-call? – cutsoy May 03 '11 at 18:24
  • @Simon read this: http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/touchesBegan:withEvent: `Tells the receiver when one or more fingers touch down in a *view* or *window*.` – cutsoy May 03 '11 at 18:25
  • Great :D! Though I'ld recommend you to implement `[... release]` again and start subclassing UIView's (as it will save you a lot of memory). – cutsoy May 03 '11 at 18:30
  • @Camilo If all you did was remove the call to release, you will now have a memory leak. To fix this, you could have the view controllers be member variables of your main view controller and add the release calls to the main view controllers `-dealloc` method. Edit: or, as Tim said, you could instead use a UIView subclass rather than another UIViewController. – Simon Goldeen May 03 '11 at 18:31
  • I have save the subview as a variable in the main view controller. and I do `[myMenuSceneMenu dealloc];` in the dealloc method. It should release the memory. – Camilo Olea May 03 '11 at 18:43
  • No and no. Dealloc gets called when the variable is released, so it can be called before termination. Also, you should call `release` (which indirectly calles `dealloc`), else the memory is still allocated. – cutsoy May 03 '11 at 18:49
  • @Camilo Aack! No! Never call `-dealloc` on an object (except for `[super dealloc]` at the end of a custom class' dealloc method). In `-dealloc` you want to call `-release` on any retained member variables. You might want to take a gander at the [Memory Management Programming guide](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html) – Simon Goldeen May 03 '11 at 18:49
  • ok ok. I will call release instead of dealloc on `myMenuSceneMenu` inside `-dealloc`. And I will also go through the guide. I appreciate all your help. – Camilo Olea May 03 '11 at 18:58
  • @TimvanElsloo You seem to have misunderstood @SimonGoldeen's comment. Calling `release` has nothing to do with it. Research the responder chain for more information. – titaniumdecoy Oct 17 '11 at 23:12
0

I am pretty sure the problem is when you call [super touchesBegan:touches withEvent:event];

From the UIResponder documentation for that method:

The default implementation of this method does nothing. However immediate UIKit subclasses of UIResponder, particularly UIView, forward the message up the responder chain.

So, the default behavior of UIViewController is to pass the touch up the responder chain. Since this isn't something you want to do (at least not immediately) you should either remove that line from your code, or include it after you have determined that you don't need or want to respond to the touch in your current view.

Simon Goldeen
  • 9,080
  • 3
  • 36
  • 45
  • Removing that line didnt work, I actually had the original method without that line but added it hoping it would help – Camilo Olea May 03 '11 at 18:02
  • Removing that line will work the opposite. Since super *checks* what subviews should be called, some subviews will now not be called anymore. You also should note that iOS is working from root to detail meaning that it will start at your UIWindow and eventually finishes in the deepest nested UIView that contains the location of the particular touch. – cutsoy May 03 '11 at 18:06