开发者

sub uiview's UIViewcontroller not detecting touches

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");

}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜