removefromsuperview 会释放scrollview 的对象吗?

时间:2022-11-15
本文介绍了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 的对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何以编程方式在 UIScrollView 中使用 AutoLayout 设置子视图? 下一篇:在 iOS 8 Today Extensions 中拖动手势

相关文章

最新文章