does removefromsuperview releases the objects of scrollview?(removefromsuperview 会释放scrollview 的对象吗?)
问题描述
for(UIView *subview in [scrollView subviews]) {NSLog(@"subviews Count=%d",[[scrollView subviews]count]);//[子视图发布];[子视图 removeFromSuperview];}
在上述方法中,如果我使用 [subview removeFromSuperview];
它工作正常...但如果我使用[subview release];
它崩溃了..我想知道两者是否相同或它们之间有什么区别?
@MathieuK 是正确的,但值得深入挖掘,因为它是 ObjC 中一个非常重要的概念.你不应该在一个你没有显式或隐式 -retain
的对象上调用 -release
(通过调用 三个神奇的词).您不会调用 -release
来释放对象.你调用它来释放 你 对对象的保持.scrollview 是否保留其子视图不是您的事(它确实保留了其子视图,但仍然不是您的事).-removeFromSuperview
是否调用 -release
也不是你的事.那是在滚动视图和它的子视图之间.重要的是,当你关心对象时你保留它们,当你不再关心它们时释放它们,让系统的其余部分负责保留和释放它所关心的.p>
for(UIView *subview in [scrollView subviews]) {
NSLog(@"subviews Count=%d",[[scrollView subviews]count]);
//[subview release];
[subview removeFromSuperview];
}
in the above method if i use [subview removeFromSuperview];
it works fine...but if i use
[subview release];
It crashes..i want to know that if both are same or is there any difference between them?
@MathieuK is correct, but it's worth digging deeper into this, because it's a very important concept in ObjC. You should never call -release
on an object you didn't -retain
explicitly or implicitly (by calling one of the Three Magic Words). You don't call -release
in order to deallocate an object. You call it to release the hold you have put on the object. Whether scrollview is retaining its subviews is not your business (it does retain its subviews, but its still not your business). Whether -removeFromSuperview
calls -release
is also not your business. That's betweeen the scrollview and its subviews. All that matters is that you retain objects when you care about them and release them when you stop caring about them, and let the rest of the system take care of retaining and releasing what it cares about.
这篇关于removefromsuperview 会释放scrollview 的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:removefromsuperview 会释放scrollview 的对象吗?
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01