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

Commit

Permalink
Added nib caching support.
Browse files Browse the repository at this point in the history
Build graph is wrapped by an autorelease pool because the recursive
function it calls loads the view and returns an autoreleased array.
  • Loading branch information
epreston committed Sep 15, 2011
1 parent ecbcc77 commit f9e91ef
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
4 changes: 4 additions & 0 deletions Example 1/PSHTreeGraph.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = NO; GCC_WARN_ABOUT_MISSING_PROTOTYPES = NO;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
INFOPLIST_FILE = "PSHTreeGraph-Info.plist"; INFOPLIST_FILE = "PSHTreeGraph-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = PSHTreeGraph; PRODUCT_NAME = PSHTreeGraph;
}; };
name = Debug; name = Debug;
Expand All @@ -375,6 +376,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = NO; GCC_WARN_ABOUT_MISSING_PROTOTYPES = NO;
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
INFOPLIST_FILE = "PSHTreeGraph-Info.plist"; INFOPLIST_FILE = "PSHTreeGraph-Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = PSHTreeGraph; PRODUCT_NAME = PSHTreeGraph;
VALIDATE_PRODUCT = YES; VALIDATE_PRODUCT = YES;
}; };
Expand Down Expand Up @@ -418,6 +420,7 @@
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = NO; GCC_WARN_UNUSED_PARAMETER = NO;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
SDKROOT = iphoneos; SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2; TARGETED_DEVICE_FAMILY = 2;
USER_HEADER_SEARCH_PATHS = ../PSTreeGraphView/; USER_HEADER_SEARCH_PATHS = ../PSTreeGraphView/;
Expand All @@ -438,6 +441,7 @@
GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_PARAMETER = NO; GCC_WARN_UNUSED_PARAMETER = NO;
GCC_WARN_UNUSED_VARIABLE = YES; GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 4.3;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1"; OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
SDKROOT = iphoneos; SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = 2; TARGETED_DEVICE_FAMILY = 2;
Expand Down
2 changes: 1 addition & 1 deletion PSTreeGraphView/PSBaseTreeGraphView.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ typedef NSInteger PSTreeGraphOrientationStyle;
BOOL _showsSubtreeFrames; BOOL _showsSubtreeFrames;


// iOS 4 and above ONLY // iOS 4 and above ONLY
// UINib *cachedNodeViewNib; UINib *_cachedNodeViewNib;


// Custom input view support // Custom input view support
UIView *_inputView; UIView *_inputView;
Expand Down
104 changes: 59 additions & 45 deletions PSTreeGraphView/PSBaseTreeGraphView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ - (void) awakeFromNib
- (void) dealloc - (void) dealloc
{ {
// iOS 4.0 and above ONLY // iOS 4.0 and above ONLY
// [cachedNodeViewNib release]; [_cachedNodeViewNib release];


self.delegate = nil; self.delegate = nil;


Expand Down Expand Up @@ -115,7 +115,7 @@ - (void)setNodeViewNibName:(NSString *)newName
if (_nodeViewNibName != newName) { if (_nodeViewNibName != newName) {


// iOS 4.0 and above ONLY // iOS 4.0 and above ONLY
// [self setCachedNodeViewNib:nil]; [self setCachedNodeViewNib:nil];


[_nodeViewNibName release]; [_nodeViewNibName release];
_nodeViewNibName = [newName copy]; _nodeViewNibName = [newName copy];
Expand All @@ -131,7 +131,7 @@ - (void) setNodeViewNibBundle:(NSBundle *)newBundle
if (_nodeViewNibBundle != newBundle) { if (_nodeViewNibBundle != newBundle) {


// iOS 4.0 and above ONLY // iOS 4.0 and above ONLY
// [self setCachedNodeViewNib:nil]; [self setCachedNodeViewNib:nil];


[_nodeViewNibBundle release]; [_nodeViewNibBundle release];
_nodeViewNibBundle = [newBundle retain]; _nodeViewNibBundle = [newBundle retain];
Expand All @@ -143,18 +143,29 @@ - (void) setNodeViewNibBundle:(NSBundle *)newBundle


#pragma mark - Node View Nib Caching #pragma mark - Node View Nib Caching


// iOS 4.0 and above ONLY // iOS 4.0 and above ONLY -


//- (UINib *)cachedNodeViewNib { - (UINib *)cachedNodeViewNib {
// return cachedNodeViewNib; return _cachedNodeViewNib;
//}
// // If 3.x Support required, change references to UINib to ID (careful not to modify
//- (void)setCachedNodeViewNib:(UINib *)newNib { // the one below.) uncomment the following code, test. Note: Does not check for valid
// if (cachedNodeViewNib != newNib) { // [self nodeViewNibName].
// [cachedNodeViewNib release];
// cachedNodeViewNib = [newNib retain]; // if (!_cachedNodeViewNib) {
// Class cls = NSClassFromString(@"UINib");
// if ([cls respondsToSelector:@selector(nibWithNibName:bundle:)]) {
// _cachedNodeViewNib = [[cls nibWithNibName:[self nodeViewNibName] bundle:[NSBundle mainBundle]] retain];
// }
// } // }
//} }

- (void)setCachedNodeViewNib:(UINib *)newNib {
if (_cachedNodeViewNib != newNib) {
[_cachedNodeViewNib release];
_cachedNodeViewNib = [newNib retain];
}
}




#pragma mark - Selection State #pragma mark - Selection State
Expand Down Expand Up @@ -232,34 +243,24 @@ - (PSBaseSubtreeView *) newGraphForModelNode:(id <PSTreeGraphModelNode> )modelNo
PSBaseSubtreeView *subtreeView = [[PSBaseSubtreeView alloc] initWithModelNode:modelNode]; PSBaseSubtreeView *subtreeView = [[PSBaseSubtreeView alloc] initWithModelNode:modelNode];
if (subtreeView) { if (subtreeView) {



// Get nib from which to load nodeView. // Get nib from which to load nodeView.
// NSNib *nodeViewNib = [self cachedNodeViewNib]; UINib *nodeViewNib = [self cachedNodeViewNib];


// if (nodeViewNib == nil) { if (nodeViewNib == nil) {
// NSString *nibName = [self nodeViewNibName]; NSString *nibName = [self nodeViewNibName];
// NSAssert(nibName != nil, @"You must set a non-nil nodeViewNibName for TreeGraph to be able to build its view tree"); NSAssert(nibName != nil,
// if (nibName != nil) { @"You must set a non-nil nodeViewNibName for TreeGraph to be able to build its view tree");
// nodeViewNib = [[NSNib alloc] initWithNibNamed:[self nodeViewNibName] bundle:[self nodeViewNibBundle]]; if (nibName != nil) {
// [self setCachedNodeViewNib:nodeViewNib]; nodeViewNib = [UINib nibWithNibName:[self nodeViewNibName] bundle:[NSBundle mainBundle]];
// } [self setCachedNodeViewNib:nodeViewNib];
// } }

}
NSArray *nibViews = nil;
NSString *nibName = [self nodeViewNibName];

NSAssert(nibName != nil, @"You must set a non-nil nodeViewNibName for TreeGraph to be able to build its view tree");

if (nibName != nil) {

// nibViews = [[self nodeViewNibBundle] loadNibNamed:[self nodeViewNibName] owner:subtreeView options:nil];
nibViews = [[NSBundle mainBundle] loadNibNamed:[self nodeViewNibName] owner:subtreeView options:nil];
}


// Instantiate the nib to create our nodeView and associate it with the subtreeView (the nib's owner). NSArray *nibViews = nil;
// TODO: Keep track of topLevelObjects, to release later. if (nodeViewNib != nil) {

// Instantiate the nib to create our nodeView and associate it with the subtreeView (the nib's owner).
//if ([nodeViewNib instantiateNibWithOwner:subtreeView topLevelObjects:nil]) { nibViews = [nodeViewNib instantiateWithOwner:subtreeView options:nil];
}


if ( nibViews ) { if ( nibViews ) {


Expand Down Expand Up @@ -305,6 +306,9 @@ - (PSBaseSubtreeView *) newGraphForModelNode:(id <PSTreeGraphModelNode> )modelNo


- (void) buildGraph - (void) buildGraph
{ {
// Auto release pool
NSAutoreleasePool *subPool = [[NSAutoreleasePool alloc] init];

// Traverse the model tree, building a SubtreeView for each model node. // Traverse the model tree, building a SubtreeView for each model node.
id <PSTreeGraphModelNode> root = [self modelRoot]; id <PSTreeGraphModelNode> root = [self modelRoot];
if (root) { if (root) {
Expand All @@ -314,6 +318,9 @@ - (void) buildGraph
[rootSubtreeView release]; [rootSubtreeView release];
} }
} }

// Drain the pool
[subPool drain];
} }




Expand Down Expand Up @@ -342,7 +349,10 @@ - (void) updateFrameSizeForContentAndClipView
newFrameSize = newMinimumFrameSize; newFrameSize = newMinimumFrameSize;
} }


self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, newFrameSize.width, newFrameSize.height ); self.frame = CGRectMake(self.frame.origin.x,
self.frame.origin.y,
newFrameSize.width,
newFrameSize.height );


} }


Expand All @@ -357,13 +367,16 @@ - (void) updateRootSubtreeViewPositionForSize:(CGSize)rootSubtreeViewSize
CGRect bounds = [self bounds]; CGRect bounds = [self bounds];


if ( [self treeGraphOrientation] == PSTreeGraphOrientationStyleHorizontal ) { if ( [self treeGraphOrientation] == PSTreeGraphOrientationStyleHorizontal ) {
newOrigin = CGPointMake([self contentMargin], 0.5 * (bounds.size.height - rootSubtreeViewSize.height)); newOrigin = CGPointMake([self contentMargin],
0.5 * (bounds.size.height - rootSubtreeViewSize.height));
} else { } else {
newOrigin = CGPointMake(0.5 * (bounds.size.width - rootSubtreeViewSize.width), [self contentMargin]); newOrigin = CGPointMake(0.5 * (bounds.size.width - rootSubtreeViewSize.width),
[self contentMargin]);
} }


} else { } else {
newOrigin = CGPointMake([self contentMargin], [self contentMargin]); newOrigin = CGPointMake([self contentMargin],
[self contentMargin]);
} }


// [(animateLayout ? [rootSubtreeView animator] : rootSubtreeView) setFrameOrigin:newOrigin]; // [(animateLayout ? [rootSubtreeView animator] : rootSubtreeView) setFrameOrigin:newOrigin];
Expand Down Expand Up @@ -594,7 +607,6 @@ - (BOOL) canBecomeFirstResponder


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ {

UITouch *touch = [touches anyObject]; UITouch *touch = [touches anyObject];
CGPoint viewPoint = [touch locationInView:self]; CGPoint viewPoint = [touch locationInView:self];


Expand Down Expand Up @@ -629,7 +641,7 @@ - (IBAction) moveToParent:(id)sender
id <PSTreeGraphModelNode> modelNode = [self singleSelectedModelNode]; id <PSTreeGraphModelNode> modelNode = [self singleSelectedModelNode];
if (modelNode) { if (modelNode) {
if (modelNode != [self modelRoot]) { if (modelNode != [self modelRoot]) {
id<PSTreeGraphModelNode> parent = [modelNode parentModelNode]; id <PSTreeGraphModelNode> parent = [modelNode parentModelNode];
if (parent) { if (parent) {
[self setSelectedModelNodes:[NSSet setWithObject:parent]]; [self setSelectedModelNodes:[NSSet setWithObject:parent]];
} }
Expand Down Expand Up @@ -725,6 +737,7 @@ - (BOOL)hasText


- (void)insertText:(NSString *)theText - (void)insertText:(NSString *)theText
{ {
// Hardware keyboard, desktop keyboard in simulator support.
if (theText && [theText length] > 0) { if (theText && [theText length] > 0) {
switch ([theText characterAtIndex:0]) { switch ([theText characterAtIndex:0]) {
case ' ': case ' ':
Expand Down Expand Up @@ -949,4 +962,5 @@ - (BOOL) modelNodeIsInAssignedTree:(id <PSTreeGraphModelNode> )modelNode
} }
} }



@end @end
4 changes: 2 additions & 2 deletions PSTreeGraphView/PSBaseTreeGraphView_Internal.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@


// Returns an UINib instance created from the TreeGraphs's nodeViewNibName and nodeViewNibBundle. // Returns an UINib instance created from the TreeGraphs's nodeViewNibName and nodeViewNibBundle.
// We automatically let go of the cachedNodeViewNib when either of these properties changes. // We automatically let go of the cachedNodeViewNib when either of these properties changes.
// Keeping a cached NSNib instance helps speed up repeated instantiation of node views. // Keeping a cached UINib instance helps speed up repeated instantiation of node views.


// @property (nonatomic, retain) UINib *cachedNodeViewNib; @property (nonatomic, retain) UINib *cachedNodeViewNib;


@end @end

0 comments on commit f9e91ef

Please sign in to comment.