I have a UIViewController then when I longpress to self.view it will push a popup (MenuViewController). But when I try to remove popup by removeFromSuperview it still appears
You can see more detail of 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];
}
Setting aside my reservations about not applying proper view controller containment, the problem is that your
handleLongPresswill be called multiple times with differentrecognizer.statevalues, once asUIGestureRecognizerStateBeganand again asUIGestureRecognizerStateEnded. You should be checking the state of the gesture, e.g.:Original Answer:
I’d suggest putting a
NSLogor breakpoint at your code with theremoveFromSuperviewand see if you’re even getting to that piece of code.There are some clear problems here. Specifically, you’re not adding added the view associated
MenuViewControllerinhandleLongPressproperly. If you want a subview with it’s own controller, you have to use containment (and that only works with iOS 5 and later). And in containment, you have critical methods likeaddChildViewController, etc. See Creating Custom Container View Controllers in the View Controller Programming Guide or see WWDC 2011 – Implementing UIViewController Containment. And, as an aside, you’re also maintaining a strong reference toMenuViewController, so even if you succeeded in removing it’s view, you’d leak the controller.Spend a little time going through the containment documentation/video, and I think you’ll want to revisit how you’re presenting your menu. This is dense reading, but worth really understanding. Containment is powerful, but has to be done right.