在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/