Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
Refactor how TUIView handles subviews, for better memory management p…
Browse files Browse the repository at this point in the history
…ractices and ARC compatibility
  • Loading branch information
jspahrsummers committed Nov 13, 2011
1 parent f37b085 commit 4df382d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/UIKit/TUIView.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ extern CGRect(^TUIViewCenteredLayout)(TUIView*);
@interface TUIView (TUIViewHierarchy)

@property (nonatomic, readonly) TUIView *superview;
@property (nonatomic, readonly, copy) NSArray *subviews;
@property (nonatomic, readonly, strong) NSArray *subviews;

/**
Recursive search, handy for debugging.
Expand Down
40 changes: 18 additions & 22 deletions lib/UIKit/TUIView.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@ - (TUIView *)closestAssociatedView


@interface TUIView ()
@property (nonatomic, copy) NSArray *subviews;
@property (nonatomic, readwrite, strong) NSMutableArray *subviews;
@end

@implementation TUIView

@dynamic subviews;
@synthesize subviews = _subviews;
@synthesize drawRect;
@synthesize layout;
@synthesize toolTip;
@synthesize toolTipDelay;

- (void)setSubviews:(NSArray *)s
{
[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];

for(TUIView *subview in s) {
[self addSubview:subview];
}
}

+ (void)initialize
{
if(self == [TUIView class]) {
Expand Down Expand Up @@ -483,11 +492,6 @@ - (TUIView *)superview
return [self.layer.superlayer closestAssociatedView];
}

- (NSArray *)subviews
{
return [self.layer.sublayers tui_map:@selector(associatedView)];
}

- (NSInteger)deepNumberOfSubviews
{
NSInteger n = [self.subviews count];
Expand All @@ -496,19 +500,6 @@ - (NSInteger)deepNumberOfSubviews
return n;
}

- (void)setSubviews:(NSArray *)s
{
NSMutableArray *toRemove = [NSMutableArray array];
for(CALayer *sublayer in self.layer.sublayers) {
[toRemove addObject:[sublayer associatedView]];
}
[toRemove makeObjectsPerformSelector:@selector(removeFromSuperview)];

for(TUIView *subview in s) {
[self addSubview:subview];
}
}

- (void)_cleanupResponderChain // called when a view is about to be removed from the heirarchy
{
[self.subviews makeObjectsPerformSelector:@selector(_cleanupResponderChain)]; // call this first because subviews may pass first responder responsibility up to the superview
Expand All @@ -529,10 +520,12 @@ - (void)removeFromSuperview // everything should go through this
if(superview) {
[superview willRemoveSubview:self];
[self willMoveToSuperview:nil];

[superview.subviews removeObjectIdenticalTo:self];
[self.layer removeFromSuperlayer];
self.nsView = nil;

[self didMoveToSuperview];
[self autorelease]; // 'release'?
}
}

Expand Down Expand Up @@ -591,7 +584,10 @@ - (void)setNextResponder:(NSResponder *)r
}

#define PRE_ADDSUBVIEW \
[view retain]; \
if (!_subviews) \
_subviews = [[NSMutableArray alloc] init]; \
\
[self.subviews addObject:view]; \
[view removeFromSuperview]; /* will call willAdd:nil and didAdd (nil) */ \
[view willMoveToSuperview:self]; \
view.nsView = _nsView;
Expand Down

0 comments on commit 4df382d

Please sign in to comment.