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 的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
iOS UIScrollView 延迟加载iOS UIScrollView Lazy Loading(iOS UIScrollView 延迟加载)
如何使用或不使用 ScrollView 使 ImageView 可缩放?how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可缩放?)
UIImageView 在 UIScrollView 中缩放和捏合UIImageView zoom and pinch in UIScrollView(UIImageView 在 UIScrollView 中缩放和捏合)
如何在 iphone 应用程序开发中的导航栏上添加 1How can i add more than 10 buttons on a navigationbar in iphone application development?(如何在 iphone 应用程序开发中的导航栏上添加 10
UIScrollView 只能用一根手指滚动UIScrollView scrolling only with one finger(UIScrollView 只能用一根手指滚动)
在 UIScrollView 中使用 UITouchUsing UITouch inside a UIScrollView(在 UIScrollView 中使用 UITouch)