4

我有一个UIViewController然后当我长按self.view它时会弹出一个弹出窗口(MenuViewController)。但是当我尝试删除弹出窗口时,removeFromSuperview它仍然出现

您可以通过此http://www.youtube.com/watch?v=nVVgmeJEnnY查看我的问题的更多详细信息

视图控制器.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];
}

菜单视图控制器.m

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

3 回答 3

2
[self.view removeFromSuperview];

你这是什么意思??????删除主视图!!!!

于 2012-12-27T05:32:43.203 回答
2

撇开我对不应用正确的视图控制器包含的保留不谈,问题是你handleLongPress将被多次调用不同的recognizer.state值,一次UIGestureRecognizerStateBegan又一次地调用UIGestureRecognizerStateEnded. 您应该检查手势的状态,例如:

-(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];
    }
}

原答案:

我建议NSLog在你的代码中放置一个或断点,removeFromSuperview看看你是否能到达那段代码。

这里有一些明显的问题。具体来说,您没有添加正确关联的MenuViewController视图handleLongPress。如果你想要一个带有它自己的控制器的子视图,你必须使用容器(这只适用于 iOS 5 和更高版本)。在遏制中,您有关键方法,例如addChildViewController,等等。请参阅View Controller Programming Guide中的Creating Custom Container View Controllers或参阅WWDC 2011 - Implementing UIViewController Containment。而且,顺便说一句,您还保持对 的强引用,因此即使您成功删除了它的视图,您也会泄漏控制器。MenuViewController

花一点时间浏览收容文件/视频,我想你会想重新审视你是如何展示你的菜单的。这是密集阅读,但值得真正理解。遏制是强大的,但必须做对。

于 2012-12-27T05:34:03.133 回答
1

而不是直接使用

[self.view removeFromSuperview];

采用

 [[self.view.superview subviews] makeObjectsPerformSelector:@selector(removeFromSuperview) withObject:self.view];
于 2012-12-27T06:43:34.303 回答