我有一个UIViewController然后当我长按到self.view它会推出一个弹出窗口(MenuViewController).但是当我尝试通过removeFromSuperview删除弹出窗口时,它仍会出现
您可以在此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]; }
解决方法
抛开我对不应用正确的视图控制器包含的保留,问题是你的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]; } }
原答案:
我建议使用removeFromSuperview在你的代码中放置一个NSLog或断点,看看你是否还得到那段代码.
这里有一些明显的问题.具体来说,您没有正确添加在handleLongPress中添加与MenuViewController关联的视图.如果你想要一个带有它自己的控制器的子视图,你必须使用包含(这只适用于iOS 5及更高版本).在收容中,你有像addChildViewController
等关键方法.请参阅View Controller编程指南中的Creating Custom Container View Controllers或参见WWDC 2011 – Implementing UIViewController Containment.而且,除此之外,你还要保留对MenuViewController的强引用,所以即使你成功删除它也是如此看,你要泄漏控制器.
花一点时间浏览收容文档/视频,我想你会想重新审视你如何展示你的菜单.这是一个密集的阅读,但值得真正理解.遏制是强大的,但必须正确.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。