Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[NEW] Bind the accessory controller's representedObject to the flow v…

…iew's selectedObject
  • Loading branch information...
commit 2eed828c9fcbe17c220dadc13557440bf6a065c2 1 parent b73d80a
@mattball authored
View
9 MBCoverFlowView.h
@@ -105,7 +105,9 @@
/**
* @brief The controller which manages the receiver's accessory view.
*
- * @details The accessory view will be displayed below the flow images.
+ * @details The accessory controller's representedObject will be bound
+ * to the receiver's selectedObject. The accessory controller's
+ * view will be displayed below the flow images.
*/
@property (nonatomic, retain) NSViewController *accessoryController;
@@ -128,6 +130,11 @@
@property (nonatomic, assign) NSInteger selectionIndex;
/**
+ * @brief The receiver's front-most item.
+ */
+@property (nonatomic, assign) id selectedObject;
+
+/**
* @name Layout Support
*/
View
56 MBCoverFlowView.m
@@ -77,6 +77,8 @@ @implementation MBCoverFlowView
autoresizesItems=_autoresizesItems, imageKeyPath=_imageKeyPath,
placeholderIcon=_placeholderIcon;
+@dynamic selectedObject;
+
#pragma mark -
#pragma mark Life Cycle
@@ -502,6 +504,29 @@ - (void)setShowsScrollbar:(BOOL)flag
[self resizeSubviewsWithOldSize:[self frame].size];
}
+- (void)setAccessoryController:(NSViewController *)aController
+{
+ if (aController == self.accessoryController)
+ return;
+
+ if (self.accessoryController != nil) {
+ [self.accessoryController.view removeFromSuperview];
+ [self.accessoryController unbind:@"representedObject"];
+ [_accessoryController release];
+ _accessoryController = nil;
+ [self setNextResponder:nil];
+ }
+
+ if (aController != nil) {
+ _accessoryController = [aController retain];
+ [self addSubview:self.accessoryController.view];
+ [self setNextResponder:self.accessoryController];
+ [self.accessoryController bind:@"representedObject" toObject:self withKeyPath:@"selectedObject" options:nil];
+ }
+
+ [self resizeSubviewsWithOldSize:[self frame].size];
+}
+
#pragma mark Managing the Selection
- (void)setSelectionIndex:(NSInteger)newIndex
@@ -526,25 +551,19 @@ - (void)setSelectionIndex:(NSInteger)newIndex
[_scroller setIntegerValue:self.selectionIndex];
}
-- (void)setAccessoryController:(NSViewController *)aController
+- (id)selectedObject
{
- if (aController == self.accessoryController)
+ return [self.content objectAtIndex:self.selectionIndex];
+}
+
+- (void)setSelectedObject:(id)anObject
+{
+ if (![self.content containsObject:anObject]) {
+ NSLog(@"[MBCoverFlowView setSelectedObject:] -- The view does not contain the specified object.");
return;
-
- if (self.accessoryController != nil) {
- [self.accessoryController.view removeFromSuperview];
- [_accessoryController release];
- _accessoryController = nil;
- [self setNextResponder:nil];
}
- if (aController != nil) {
- _accessoryController = [aController retain];
- [self addSubview:self.accessoryController.view];
- [self setNextResponder:self.accessoryController];
- }
-
- [self resizeSubviewsWithOldSize:[self frame].size];
+ self.selectionIndex = [self.content indexOfObject:anObject];
}
#pragma mark Layout Support
@@ -813,4 +832,11 @@ - (void)layoutSublayersOfLayer:(CALayer *)layer
}
}
+#pragma mark NSKeyValueObserving
+
++ (NSSet *)keyPathsForValuesAffectingSelectedObject
+{
+ return [NSSet setWithObjects:@"selectionIndex", nil];
+}
+
@end
View
8 MBCoverFlowViewController.m
@@ -42,6 +42,7 @@ - (void)awakeFromNib
{
NSImage *image = [[NSImage alloc] initWithContentsOfFile:[@"/Users/matt/Pictures/Photo Booth" stringByAppendingPathComponent:file]];
if (image != nil) {
+ [image setName:file];
[images addObject:image];
}
[image release];
@@ -57,12 +58,17 @@ - (void)awakeFromNib
[label setBezeled:NO];
[label setEditable:NO];
[label setSelectable:NO];
- [label setObjectValue:@"Test"];
[label setDrawsBackground:NO];
[label setTextColor:[NSColor whiteColor]];
[label setFont:[NSFont boldSystemFontOfSize:12.0]];
+ [label setAutoresizingMask:NSViewWidthSizable];
+ [label setAlignment:NSCenterTextAlignment];
[label sizeToFit];
+ NSRect labelFrame = [label frame];
+ labelFrame.size.width = 400;
+ [label setFrame:labelFrame];
[labelViewController setView:label];
+ [label bind:@"value" toObject:labelViewController withKeyPath:@"representedObject.name" options:nil];
[label release];
[(MBCoverFlowView *)self.view setAccessoryController:labelViewController];
[labelViewController release];
Please sign in to comment.
Something went wrong with that request. Please try again.