ios - UIView 将如何获得释放?解释

标签 ios objective-c uiview nsarray automatic-ref-counting

在ARC中,我想释放添加到父 View 和数组的自定义 View 。

@property (nonatomic, weak) IBOutlet UIScrollView* panelScroll;
@property (nonatomic, retain) NSMutableArray *medsSectionViews;

上面是添加了customview对象的两个属性如下:

CustomView* newView;
newView = [[CustomView alloc] init];
[panelScroll addSubview:newView];
[self.medsSectionViews addObject:newView];
newView = nil;

它没有释放对象。问题是什么?我怎样才能实现它?请在需要的地方提及引用计数。

最佳答案

CustomView *newView = [[CustomView alloc] init]; // 1 owner (the newView local variable)
[panelScroll addSubview:newView];  // 2 owners (local variable, the superview panelScroll)
[self.medsSectionViews addObject:newView]; // 3 owners (local variable, superview, array medsSectionViews)
newView = nil;  // 2 owners (superview, array)

现在

[array removeAllObjects]; // 1 owner (superview)
[newView removeFromSuperview]; // 0 owners
// view gets deallocated because it has no owners

关于ios - UIView 将如何获得释放?解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43756856/

相关文章:

iphone - 使 UIImagePickerController (相机 View )仅显示一定数量的像素

ios - 添加延迟动画时,UIView 动画不起作用

ios - 在主视图 Swift 中嵌入 subview

ios - 带有 Xcode 8 beta : Found an unexpected Mach-O header code: 0x72613c21 的 AWS

ios - 高级自动完成 Swift

ios - iOS 10 中的 UITableView 标题 View 重叠单元格

ios - 在 Swift 中用数组序列化我自己的对象的更优雅的方法是什么

ios - NSDateFormatter 以看似正确的格式字符串返回 Nil

ios - 将框架/边界/约束从一个 View 复制到另一个 View

ios - 如何在uiview中进行西装搭配和排名?