Having thrown away my reservations that you are not using the proper controller shell, the problem is that your handleLongPress will be called multiple times with different recognizer.state values, once as UIGestureRecognizerStateBegan and again as UIGestureRecognizerStateEnded . You should check the status of the gestures, for example:
-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer { if (recognizer.state == UIGestureRecognizerStateEnded) { menu = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil]; if (self.imageView.image != nil) { menu.imageAdded = YES; } [self.view addSubview:menu.view]; } }
Original answer:
I would suggest placing an NSLog or breakpoint in your code with removeFromSuperview and see if you fall into this part of the code.
There are some obvious issues here. In particular, you are not adding the added view associated with the MenuViewController to handleLongPress correctly. If you want a subview with its own controller, you should use containment (and this only works with iOS 5 and later). And in containment, you have critical methods like addChildViewController etc. See Creating Custom Container View Controllers in the View Controller Programming Guide or see WWDC 2011 - Implementing the UIViewController Containment . And, aside, you also maintain a strong link to the MenuViewController , so even if you manage to remove it, you will skip the controller.
Spend some time on the documentation / containment video, and I think you'll want to get back to the way you present your menu. It is a dense read, but worth it to understand. Containment is powerful, but must be done correctly.
source share