1
votes

Hey guys i have a a UIViewController, RootUIViewController referencing another UIViewcontroller, MainMenuViewController.

Im adding MainMenuViewController's view as a subview to RootUIViewController's view. The problem is touch events are not being caught in MainMenuViewController touchesBegan method.

Relevant code is below. The output when touching the screen show "touched at root view controller". The desired result i want is the touch event to be caught in the MainMenuViewController and display "touched at root view controller". What am I missing/ doing wrong here?

RootUIViewController.m

  - (void)viewDidLoad {
    [super viewDidLoad];

    MainMenuViewController* mainMenuViewController = [[MainMenuViewController alloc] initWithNibName:@"MainMenuView" bundle:nil];

    m_mainMenuViewController = mainMenuViewController;
    [self.view addSubview:m_mainMenuViewController.view];
    [mainMenuViewController release];

}

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touched at root view controller");

    }

MainMenuViewController.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touched at main view controller");

}
1
What are you exactly trying to do?rptwsthi
The code here looks ok. Check if the userInteractionEnabled of m_mainMenuViewController.view is set to NO.Deepak Danduprolu
@rptwsthi My goal is when i touch the view (m_mainMenuViewController.view) the event will be caught in m_mainMenuViewController touchesBegan method. @Deepak thanks ill give that a govalmo

1 Answers

2
votes

Glad you were able to resolve your memory management issue. I want to add a warning that

[self.view addSubview:m_mainMenuViewController.view];

is problematic and, in my opinion, a bad idea. Subviews of a UIViewController's view should not be managed by their own UIViewController. Those subviews can have controllers but they should not be UIViewController subclasses because they will never reliably behave exactly like you might expect from a UIViewController and that is likely to cause a headache for you later. Better to accept the limits of the classes and API we get from Apple and design a supported, reliable solution.

I've tried to cover this in detail here: Abusing UIViewControllers, hopefully that's some help.