Search code examples
objective-cuiviewtouchesbegan

touchesBegan is not called after releasing the UIView Object


I'm implementing an UIView with touchesBegan method.

When I create this UIView without releasing after add in subviews collection, all works fine.

But, when I'm a good citizen and I release the object after adding in subviews, the touches begans is not called.

This is my code:

Inserting UIViewController. OTTestResult is the Controller of the UIView:

-(void)validateWasPushed:(id)sender
{
    float nota = [testQuestions getTestResults];
    int ok = [testQuestions getOkNumber];
    int fail = [testQuestions getFailNumber];

    OTTestResult *result = [[OTTestResult alloc] init];

    [self.view addSubview:result.view];

    [result loadWithNumber:nota testName:[testQuestions testName] okResponses:ok failResponses:fail];


    [result release];

}

And touchesBegan implementation in OTTestResult object:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view removeFromSuperview];
}

Anyone knows what I'm doing wrong?

Thanks.


Solution

  • But, when I'm a good citizen and I release the object after adding in subviews, the touches begans is not called.

    You did not add result to subviews. You added result.view. You then released result, which no one else is holding, and it deallocated. You will need to hold onto the sub-view controller in an ivar if you need it to stay around.