在removeFromSuperview之后删除/释放UIView的适当方法
我正在尝试在 iOS 应用程序中绘图。我有一个类,它是 UIView 的子类,它绘制一些线条和东西。当用户按下按钮时,我会实例化该类,并在应用程序的主 UIViewController
视图上执行 addSubView
操作。用户按下该按钮的次数越多,该类的实例就越多添加到视图中。它工作得很好。
现在我想为用户提供一种删除其中一个视图的方法。到目前为止,我已将[self removeViewFromSuperview]
放入自定义UIView
的touchesBegan方法中。因此,当用户按下绘图时,它就会从视图中删除。但是,它实际上并没有被删除,对吗?由于视图是在按下按钮时执行的方法中实例化的,因此我无法从 UIViewController 中引用它。确保创建和删除的 UIView 不会浪费内存的适当方法是什么?
与此相关的是,如果我要在主窗口的 UIView 上放置一个切换删除的切换开关,我如何从 TouchBegan 中检查该切换开关是否设置为 delete=yes?我可以在 AppDelegate
中有某种布尔变量吗?我可以从 UIView
子类中检查它吗?我该如何引用呢?
谢谢您的帮助,
有状态的
I'm playing around with drawing in iOS apps. I have a class that is a subclass of UIView
that draws some lines and stuff. When the user presses a button, I instantiate the class and do an addSubView
on the view of the main UIViewController
of the app. The more times the user presses that button, the more instances of that class get added to the view. It's working just fine.
Now I want to provide the user a way to delete one of those views. So far I've put a [self removeViewFromSuperview]
into the touchesBegan method of the custom UIView
. So when the user presses the drawing it gets removed from the view. But, it's not actually deleted, right? Since the view was instantiated within the method that executes when the button is pressed I have no way to reference it from within the UIViewController
. What's the appropriate way to make sure I'm not wasting memory with a UIView
that was created and removed?
On a related note, if I was to put a toggle switch on the main window's UIView
that toggles delete, how can I check from within touchesBegan if that toggle switch is set to delete=yes? Would I have a some sort of boolean variable in the AppDelegate
that I can check from within the UIView
subclass? How would I reference that?
Thank you for your help,
Stateful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您像这样添加视图:
您可以删除它而不会泄漏内存:
关于
UISwitch
,您不需要在任何地方保留它的值,除非您需要它做其他事情。您可以直接访问它的值:您甚至不需要
IBOutlet
,您可以通过其标签访问开关:在这种情况下,您必须在 Interface Builder 中为开关设置唯一的标签号,或者当你创造它。
If you add the view like this:
You can remove it without leaking memory:
Regarding the
UISwitch
, you don't need to keep its value anywhere unless you need it for something else. You can access its value directly:You don't even need an
IBOutlet
, you can access the switch with its tag:In this case you must set a unique tag number for the switch in Interface Builder or when you create it.
当你执行[mainView addSubView:myView]时,mainView将保留myView。如果您使用 alloc/init 创建了 myView,那么您也保留了它。如果将 myView 添加到主视图后不需要它,则只需在添加后执行 [myView release] 即可。当您从主视图中删除它时,它将被释放并取消分配。
When you do [mainView addSubView:myView], mainView will retain myView. If you created myView with alloc/init, then you retained it also. If you don't need myView after adding it to the main view then simply do [myView release] after adding it. When you remove it from the main view, it will get released and deallocated.
如果您使用
alloc/init
创建UIView
,将其添加到超级视图然后释放该视图,超级视图将保留它。当使用removeViewFromSuperview
删除它时,它将被释放。If you create the
UIView
withalloc/init
, add it to the superview then release the view, the superview will retain it. When it is removed withremoveViewFromSuperview
it will be dealloc'ed.我通常在添加视图后自动释放它,将父级作为唯一的引用。
至于检查切换,您可以添加 IBOutlet,以便可以直接检查它。 (这可能不是纯MVC,但我不知道把它放在 [UIApplication sharedApplication].delegate 中是否一定更干净。)
I typically autorelease a view after adding it, leaving the parent the only reference.
As to checking a toggle, you could add an IBOutlet so you can inspect it directly. (This may not be pure MVC, but I don't know if putting it in [UIApplication sharedApplication].delegate is necessarily cleaner.)