Hello,
I am trying to understand bindings in cocoa and to that purpose I am creating an app that draws a black rectangle on a blue background using sliders. The interface is as follows :
The interface includes a custom view and 2 sliders: 1 horizontal for the black rectangle width and 1 vertical for the black rectangle height.
I have a NSView subclass that draw the black rectangle on the blue background. The black rectangle is centered and its size is half the view size. This part works and reacts well to window resizing.
I have a controller class (NSObject) with 2 float variables (sliderheight and sliderwidth). These variables are bound to their respective sliders using IB. This also works well.
The problem is that I am not sure how to send the sliders values to my NSView subclass.
My first attempt was to add the sliderheight and sliderwidth variables to the NSView subclass (and not using a controller class). However, I ended with 2 instances of my NSView subclass, one bound to the custom view and the other bound to the sliders.
I understand that I can use notifications to pass the sliders value to my NSView class but is there simpler / better way to do that ?
Thanks,
Paul
I am trying to understand bindings in cocoa and to that purpose I am creating an app that draws a black rectangle on a blue background using sliders. The interface is as follows :

The interface includes a custom view and 2 sliders: 1 horizontal for the black rectangle width and 1 vertical for the black rectangle height.
I have a NSView subclass that draw the black rectangle on the blue background. The black rectangle is centered and its size is half the view size. This part works and reacts well to window resizing.
Code:
- (void)drawRect:(NSRect)dirtyRect {
NSLog(@"drawRect called in : %p", self);
// blue background drawing
[[NSColor blueColor] set];
NSRect fond = [self bounds];
NSRectFill(fond);
// black rectangle origin and size computation
float pixabcisse, pixordonne, pixlargeur, pixhauteur;
float fondhauteur = [self bounds].size.height;
float fondlargeur = [self bounds].size.width;
pixlargeur = (fondlargeur / 100) * blackrectwidth;
pixhauteur = (fondhauteur / 100) * blackrectheight;
pixabcisse = (fondlargeur - pixlargeur) / 2 ;
pixordonne = (fondhauteur - pixhauteur) / 2 ;
//black rectangle drawing
NSRect pix;
[[NSColor blackColor] set];
pix = NSMakeRect(pixabcisse,pixordonne, pixlargeur, pixhauteur);
NSRectFill(pix);
}
I have a controller class (NSObject) with 2 float variables (sliderheight and sliderwidth). These variables are bound to their respective sliders using IB. This also works well.
The problem is that I am not sure how to send the sliders values to my NSView subclass.
My first attempt was to add the sliderheight and sliderwidth variables to the NSView subclass (and not using a controller class). However, I ended with 2 instances of my NSView subclass, one bound to the custom view and the other bound to the sliders.
I understand that I can use notifications to pass the sliders value to my NSView class but is there simpler / better way to do that ?
Thanks,
Paul
Last edited: