I have a UIScrollView with an Image subView. I subclass touchesEnded to add a dialog box as a subView as follows:
- (void) pushInfoBox {
// the following line is referred to "myVC Caller" below
myViewController *myVC = [[myViewController alloc]
initWithNibName:@"myView" bundle:nil];
[self addSubview:myVC.view];
[myVC release];
}
The view shows correctly, but when I click myVC.view.backButton the code execution jumps to the “myVC Caller” line above. The next step dumps with -[myViewController performSelector:withObject:withObject:]: message sent to deallocated instance.
Strangely the myCaller.IBAction (backButton action) is not executed.
I expect the user interaction enabled between the scrollview and the pushed message box interfere with each other, but am unsure how to handle this correctly.
any ideas on how to tackle this?
There are several major issues with your code. I’ll walk through it:
- (bool)pushInfoBox:(int)xPos down:(int)yPos window:(int)wPos {First of all, you’re not returning a
BOOLvalue in this method so the return type should bevoid(if it was returning aBOOL, note that its uppercase, not lowercase as you have it now). Second, you are supplyingxPos,yPos, andwPosas arguments but they are not being used in the method. So your method declaration should look more like this:- (void)pushInfoPox;The next problem is here:
myViewController *myVC = [[myViewController alloc]initWithNibName:@"myView" bundle:nil];
You are allocating an instance of
myViewController(by the way, class names should always have their first letter capitalized) but never releasing it, so you are causing a memory leak. You should be declaringmyVCas an instance variable and a retained property in your header:You should be allocating it like this now:
myVC = [[myViewController alloc]initWithNibName:@"myView" bundle:nil];
And it should be released in the
deallocmethod:The last problem is in this line:
[mBox release];I don’t see any object called
mBoxbeing retained in that method, so that line should not be there.