UIViewController error removeFromSuperview

I have a UIViewController , and then when I click on self.view , it pops up a popup ( MenuViewController ). But when I try to remove the removeFromSuperview , it still appears

You can see in more detail my problem with this http://www.youtube.com/watch?v=nVVgmeJEnnY

ViewController.m

 #import "MenuViewController.h" @interface ViewController () { MenuViewController *menu; } .... - (void)viewDidLoad { .... [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(albumButtonPressed : ) name:@"albumButtonPressed" object:nil]; .... } .... -(void)albumButtonPressed : (NSNotification*) notification { UIImagePickerController *photoPicker = [[UIImagePickerController alloc] init]; photoPicker.delegate = self; photoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; [self presentModalViewController:photoPicker animated:YES]; } ... -(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer { menu = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil]; if (self.imageView.image != nil) { menu.imageAdded = YES; } [self.view addSubview:menu.view]; } 

MenuViewController.m

 -(IBAction)albumButtonPressed:(id)sender { [self.view removeFromSuperview]; [[NSNotificationCenter defaultCenter] postNotificationName:@"albumButtonPressed" object:nil]; } 
+4
source share
3 answers

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.

+2
source
 [self.view removeFromSuperview]; 

what do you mean by that ?????? removal of the main view !!!!

+2
source

Instead of direct use

 [self.view removeFromSuperview]; 

using

  [[self.view.superview subviews] makeObjectsPerformSelector:@selector(removeFromSuperview) withObject:self.view]; 
+1
source

All Articles